【问题标题】:ERROR 403 Forbidden YouTube Analytics API错误 403 禁止 YouTube 分析 API
【发布时间】:2020-06-21 03:55:17
【问题描述】:

我正在尝试使用 YouTube 分析 API 获取报告,但 403 禁止 让我卡住了。我在谷歌上搜索了很多,但没有找到相关的解决方案。我正在使用 google oAuth 2.0 尝试其官方文档中的代码。

import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtubeAnalytics"
    api_version = "v2"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube_analytics = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube_analytics.reports().query(
        dimensions="day",
        endDate="2018-12-01",
        ids="channel==UCzxqTj9GZVeSy_Er9VPvKEQ",
        metrics="views",
        startDate="2018-11-29"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()
Error

{
  "error": {
    "code": 403,
    "message": "Forbidden",
    "errors": [
      {
        "message": "Forbidden",
        "domain": "global",
        "reason": "forbidden"
      }
    ]
  }
}

【问题讨论】:

    标签: python youtube-analytics-api


    【解决方案1】:

    HTTP 403 响应可能表明您尝试使用的 Google 帐户的某些部分未得到正确授权/配置。您可能希望转到您的Google cloud console 并检查您是否拥有:

    Enabled the YouTube Analytics API 用于您想要使用它的项目。 关注Authorization flow for the API。需要注意的是,目前尚不支持通过服务帐号访问渠道数据(但可以代表其他用户访问分析数据。)

    API 摘录(2021 年 6 月):

    • YouTube 分析 API 不支持服务帐户流程。
    • YouTube Reporting API 仅支持服务帐户流 拥有和管理多个 YouTube 频道的 YouTube 内容所有者。 具体来说,内容所有者可以在 API 请求中使用服务帐户 为 onBehalfOfContentOwner 请求参数设置一个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2015-04-19
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2022-07-15
      相关资源
      最近更新 更多