我正在尝试使用 Youtube Analytics API 获取有关我的频道表现的每月分析。根据这个 url,它列出了我可以访问的各种指标,包括
estimatedMinutesWatched, views, likes, subscribersGained, estimatedRevenue
等。当我进行指定这些指标的 api 调用时 estimatedMinutesWatched, views, likes, subscribersGained
我得到了有效的响应,并且可以看到指标根据当天进行细分。但是,当我添加指标estimatedRevenue时,它会出现以下错误:
Traceback (most recent call last):
File "C:\Users\ayubs\OneDrive\Desktop\youtube-analytics\youtube_analytics_api_monthly_stats.py", line 104, in <module>
execute_api_request(
File "C:\Users\ayubs\OneDrive\Desktop\youtube-analytics\youtube_analytics_api_monthly_stats.py", line 92, in execute_api_request
response = client_library_function(
File "C:\Python\Python39\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python\Python39\lib\site-packages\googleapiclient\http.py", line 840, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtubeanalytics.googleapis.com/v2/reports?ids=channel%3D%3DMINE&startDate=2022-01-30&endDate=2022-02-15&metrics=estimatedMinutesWatched%2Cviews%2Clikes%2CsubscribersGained%2CestimatedRevenue&dimensions=day&sort=day&alt=json returned "Forbidden">
这是我拨打电话的方式,我下载了 OAuth 2.0 客户端 ID 凭据,该凭据存储在 GCP 的 json 中。我正在将这些导入到我的代码中,执行时要求我登录我的电子邮件/youtube 帐户。
我粘贴了以下代码的副本:
import os
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPES = [
'https://www.googleapis.com/auth/yt-analytics.readonly',
'https://www.googleapis.com/auth/yt-analytics-monetary.readonly',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtubepartner'
]
API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'client_secret.json'
def get_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
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)
if __name__ == '__main__':
# Disable OAuthlib's HTTPs verification when running locally.
# *DO NOT* leave this option enabled when running in production.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
youtubeAnalytics = get_service()
execute_api_request(
youtubeAnalytics.reports().query,
ids='channel==MINE',
startDate='2022-01-30',
endDate='2022-02-15',
metrics='estimatedMinutesWatched,views,likes,subscribersGained,estimatedRevenue',
dimensions='day',
sort='day'
)
也许我的处理方式是错误的?我只是希望能够提取有关我的频道的货币信息,无论是与广告相关还是仅与估计收入相关。
我使用的范围如下,我已将这些范围添加到 GCP 中:
[
'/auth/yt-analytics.readonly',
'/auth/yt-analytics-monetary.readonly',
'/auth/youtube.readonly',
'/auth/youtube',
'/auth/youtubepartner'
]
我知道某些功能可能已被弃用,但我尽力使用最新的 API,如果您发现任何问题,请指导。谢谢。
禁止意味着您无权执行您想要执行的操作。您可能已发送访问令牌,但要访问该数据,创建该令牌的用户必须有权访问该数据。