2016-10-13 183 views
1

我试图连接到使用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帐户的管理员。

+0

您是否已通过IP – MatejMecka

+0

@UnknownDeveloper启用了从控制台进行访问?我不确定该连接到Google Search Console API时究竟该如何执行该操作,但不会得到同样的错误?我对Search Console API没有任何问题,只是YouTube Analytics API。仅供参考,我在我的问题中添加了更多信息,描述了使用OAuth方法时发生的情况。 – Abundnce10

回答

2

我对这个documentation,你不能使用与YouTube Analytics(分析)API服务帐户中。

在此指出:

The service account flow supports server-to-server interactions that do not access user information. However, the YouTube Analytics API does not support this flow. Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate an error.

我还发现它在这个thread由Google员工回答。

欲了解更多信息,请检查该SO question

+0

很好找,KENdi!在阅读授权文档的服务帐户部分时,我没有注意到。那么,我是否必须使用OAuth进行身份验证?这可能来自运行cron作业的服务器吗? – Abundnce10

相关问题