【问题标题】:Trying to access an account's Youtube data via the Youtube Reporting API results in HttpError 403: "The caller does not have permission"尝试通过 Youtube 报告 API 访问帐户的 Youtube 数据会导致 HttpError 403:“调用者没有权限”
【发布时间】:2019-03-30 09:26:25
【问题描述】:

我正在尝试使用 Youtube 的报告 API 访问我公司的 Youtube 数据并对其进行分析。但是,当我运行 Google 提供的示例脚本时,我收到以下错误:

HttpError: <HttpError 403 when requesting https://youtubereporting.googleapis.com/v1/media/%20?alt=json returned "The caller does not have permission">

我创建了一个 Oauth 2.0 客户端 ID,下载了客户端机密文件,并将脚本指向该文件。我也被重定向到谷歌的授权页面,在那里我登录到我试图从中获取数据并授权脚本的 Youtube 帐户。作为参考,我使用的范围是:

https://www.googleapis.com/auth/yt-analytics-monetary.readonly

这是我第一次尝试使用 Google 的 API,所以我确定我遗漏了一些东西,但我似乎无法弄清楚。如果有人可以提供一些指导,我将不胜感激。

编辑:我在下面的 Google 示例脚本中包含了我正在使用的代码。

import urllib
import json
import os
from io import FileIO

import google.oauth2.credentials
import google_auth_oauthlib.flow
from oauth2client import client
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import MediaIoBaseDownload

def get_service():
    flow = InstalledAppFlow.from_client_secrets_file(client_secrets_file=client_secrets, scopes=scope)
    credentials = flow.run_console()
    return build(api_service_name, api_version, credentials = credentials)

def execute_api_request(client_library_function, **kwargs):
    response = client_library_function(**kwargs).execute()

    print(response)

scope = ['https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
api_service_name = 'youtubereporting'
api_version = 'v1'
client_secrets = 'client_secret_397754690264-ba1rtbpi731qkveqb11361q4ggui2bmd.apps.googleusercontent.com.json'

youtube_analytics = get_service()
youtube_analytics.jobs().create(body={'reportTypeId':'channel_combined_a2', 'name':'test_job'}).execute()

# this lists all the reports that are generated after you've created a job
# so it might take a while before you're able to get list a report
youtube_analytics.jobs().reports().list(jobId = job_id).execute()

# this uses a reportId from one of the reports that are listed above
report_url = youtube_analytics.jobs().reports().get(jobId = job_id, reportId = 'REPORTIDGOESHERE').execute()['downloadUrl']

request = youtube_analytics.media().download(resourceName = " ")
request.uri = report_url

fh = FileIO('yt_test.txt', mode='wb')
downloader = MediaIoBaseDownload(fh, request, chunksize=-1)

done = False
while done is False:
    status, done = downloader.next_chunk()
    if status:
        print('Download %d%%.' % int(status.progress() * 100))
print('Download Complete!')

编辑#2:只是想用我为可能偶然发现它的任何人找到的解决方案来更新它。

问题是我没有使用在您创建作业时创建的报告 url 设置请求的 uri,而该作业又会生成报告。我假设这是它自己的报告,但实际上这只是您下载所述报告的方法。所以,我继续创建了一个生成报告的工作。然后,我从报告元数据中获取“downloadUrl”,将请求的 uri 设置为 url,然后像往常一样运行下载器。我已经更新了上面的代码以反映这一点。

【问题讨论】:

  • 如果您正在运行 google 的示例脚本并且它失败了,这听起来像是 google 支持的问题,而不是 stackoverflow。除非您提供正在运行的代码,否则我们无法为您提供帮助。
  • 我认为没有必要提供我的代码,因为我认为这是我如何授权脚本而不是编码问题的问题,但我已经编辑了我的帖子以包含代码不管。
  • 您是否有理由无法从谷歌那里得到关于他们的示例代码为什么不起作用的答案? Here 是关于同一错误的另一个 SO 问题,也许这也会有所帮助
  • 谢谢,我去看看有没有帮助。我没有问 Google,因为我认为这不是他们的示例代码有问题,而是我授权我的应用程序的方式有问题。

标签: python youtube-api youtube-analytics-api


【解决方案1】:

添加此内容以便我可以将其标记为已回答。我的解决方案如下:

问题是我没有使用在您创建作业时创建的报告 url 设置请求的 uri,而该作业又会生成报告。我假设这是它自己的报告,但实际上这只是您下载所述报告的方法。所以,我继续创建了一个生成报告的工作。然后,我从报告元数据中获取“downloadUrl”,将请求的 uri 设置为 url,然后像往常一样运行下载器。我已经更新了上面的代码以反映这一点。

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2017-04-06
    • 2015-02-12
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多