我试图连接到使用Python在YouTube Analytics(分析)API。当我访问Credentials标签中的Google开发者控制台时,我点击Create credentials
下拉菜单并选择Help me choose
。我点击我想使用的API(YouTube Analytics API
),我将从(Other non-UI (e.g. cron job, daemon)
)拨打电话,我将访问哪些数据(Application Data
),然后是否使用Google App Engine(no
)。我点击按钮查看我需要哪些凭据,它告诉我You alread have credentials that are suitable for this purpose
。我使用Service account
连接到Google Search Console API,以访问我公司拥有的多个网站的数据。由于我们有多个网站,因此我使用基于我的电子邮件地址的委派凭据。这是我使用验证到搜索控制台API代码:谷歌API授权(服务帐户)错误:HttpAccessTokenRefreshError:unauthorized_client:未经授权客户端或范围,要求
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
webmasters_service = build('webmasters', 'v3', http=http_auth)
现在,我尝试使用类似的方法使用YouTube Analytics(分析)API,但我得到这个错误:HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.
。这里是我的代码:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
START_DATE = "2016-09-01"
END_DATE = "2016-09-30"
scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
youtube_service = build('youtubeAnalytics', 'v1', http=http_auth)
analytics_query_response = youtube_service.reports().query(
ids="channel==<my-youtube-channel-id>",
metrics="views",
start_date=START_DATE,
end_date=END_DATE
).execute()
的keyfile.json
是相同的文件(包含服务帐户凭据),我用它来连接到Search Console的API。我甚至尝试创建一个新的服务帐户,并使用这些凭据,但我没有任何运气。是的,我已经在开发者控制台中启用了所有的YouTube API。
你知道我为什么会收到HttpAccessTokenRefreshError: unauthorized_client: ...
错误吗?
编辑:此前我使用OAuth ID而不是服务帐户,当我运行我的脚本时浏览器选项卡出现了,我不得不选择一个帐户。我有两种选择:一种是我的电子邮件,另一种是我作为经理加入的YouTube帐户。您是否认为我正在使用我的电子邮件地址来生成凭据(而不是YouTube帐户)?
edit2:看来YouTube帐户可能不属于我们Google Apps网域的保护范围。所以,这可能是我为什么不能授权使用我的电子邮件地址,尽管我已经成为该电子邮件地址的YouTube帐户的管理员。
您是否已通过IP – MatejMecka
@UnknownDeveloper启用了从控制台进行访问?我不确定该连接到Google Search Console API时究竟该如何执行该操作,但不会得到同样的错误?我对Search Console API没有任何问题,只是YouTube Analytics API。仅供参考,我在我的问题中添加了更多信息,描述了使用OAuth方法时发生的情况。 – Abundnce10