Livebroadcast未绑定到liveStream,如何绑定它?

问题描述 投票:0回答:1

如果我使用以下代码,则会从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
ruby-on-rails youtube-api youtube-data-api
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.