2016-10-13 152 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

相關問題