Livebroadcast没有绑定到liveStream,如何绑定它?

时间:2019-12-11 10:59:04

标签: ruby-on-rails youtube-api youtube-data-api

如果我使用以下代码,则会从Youtube API返回有效的响应(无错误)。 似乎只有流没有绑定。

def bind_broadcast_to_stream(broadcast_id, livestream_id)
  data = { empty: "string" }
  begin
    request = RestClient.post(
      "https://www.googleapis.com/youtube/v3/liveBroadcasts/bind?key=#{GOOGLE_API_KEY}&part=id,snippet,contentDetails,status&id=#{broadcast_id}&stream_id=#{livestream_id}",
      data.to_json,
      content_type: :json,
      accept: :json,
      authorization: "Bearer #{self.get_token}"
    )
    return JSON.parse(request)
  rescue RestClient::BadRequest => err
    return err.response.body
  end
end

我可以去Youtube工作室手动绑定它,但是然后我得到了另一个流密钥。 之后(当然还有流式传输),我可以使用以下代码进行直播:

def set_broadcast_status(broadcast_id, status)
  data = { empty: "string" }
  begin
    request = RestClient.post(
      "https://www.googleapis.com/youtube/v3/liveBroadcasts/transition?key=#{GOOGLE_API_KEY}&part=id,snippet,contentDetails,status&alt=json&id=#{broadcast_id}&broadcastStatus=#{status}",
      data.to_json,
      content_type: :json,
      accept: :json,
      authorization: "Bearer #{self.get_token}"
    )
    return JSON.parse(request)
  rescue RestClient::BadRequest => err
    return err.response.body
  end
end

1 个答案:

答案 0 :(得分:0)

似乎我要添加一个响应正文。

根据Youtube API手册(https://developers.google.com/youtube/v3/live/docs/liveBroadcasts/bind): 调用此方法时,请勿提供请求正文。

def bind_broadcast(broadcast_id, livestream_id)
  begin
    request = RestClient::Request.execute(
      method: :post,
      url: "https://www.googleapis.com/youtube/v3/liveBroadcasts/bind",
      headers: {
        params: { key: GOOGLE_API_KEY, part: "id,snippet,contentDetails,status", id: broadcast_id, streamId: livestream_id, alt: 'json' },
        content_type: :json,
        accept: :json,
        authorization: "Bearer #{self.get_token}"
      }
    )
    return JSON.parse(request)
  rescue RestClient::BadRequest => err
    return err.response.body
  end
end