com.google.api.client.googleapis.json.GoogleJsonResponseException:403 禁止 - YouTube 直播

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden - YouTube Live

我是 YouTube API 的新手。我已经在 YouTube 上创建和获取现场活动。但我正在尝试从我的应用程序开始直播活动,但出现此错误:

 com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
POST https://www.googleapis.com/youtube/v3/liveBroadcasts/transition?broadcastStatus=live&id=m0BCwKmG9Ws&key=AIzaSyCRiIM7Tdb_rqFcGSbQ0jXy0GeODCNSq4g&part=status
{
  "code": 403,
  "errors": [
    {
      "domain": "youtube.liveBroadcast",
      "message": "Stream is inactive",
      "reason": "errorStreamInactive",
      "extendedHelp": "https://developers.google.com/youtube/v3/live/docs/liveBroadcasts/transition"
    }
  ],
  "message": "Stream is inactive"
}

上线代码

fun startEvent(youtube: YouTube, broadcastId: String?) {
   
    val transitionRequest = youtube.liveBroadcasts().transition(
        "live", broadcastId, "status"
    )
    transitionRequest.key = context.getString(R.string.api_key)
    transitionRequest.execute()
}

我也试过 但仍然遇到同样的问题。 不知道怎么回事?

根据 LiveBroadcasts.transition API 端点的官方规范:

Before calling this method, you should confirm that the value of the status.streamStatus property for the stream bound to your broadcast is active.

error specification内容如下:

forbidden (403) errorStreamInactive
The requested transition is not allowed when the stream that is bound to the broadcast is inactive.

因此,明确地说,在调用 LiveBroadcasts.transition.

之前,绑定到直播的直播流必须处于活动状态

此错误将通过在创建实时事件时启用自动启动来解决

val contentDetails = LiveBroadcastContentDetails()
contentDetails.set("enableAutoStart", true)