我已经整理了许多关于这个话题的帖子,但大多数似乎都过时了.
我正在try 使用this script调用YouTube报告API
但是,我一直收到一个错误:
- 当使用‘桌面应用程序’OAuth时,我得到:
Error 400: invalid_request,已阻止带外(OOB)流,以确保用户安全.
- 当使用‘Web应用程序’OAuth时,我得到:
Error 400: redirect_uri_mismatch,请求中的重定向URI urn:ietf:wg:OAuth:2.0:oob只能由本机应用程序的客户端ID使用.Web客户端类型不允许这样做.
我只是还在测试我的代码,Jupyter笔记本和Visual Studio代码已经用完了.两者都有相同的错误.
我仍然有点困惑我应该使用哪一个,但是为我的本地主机添加重定向URI不起作用,不确定如何继续.
EDIT:个 我直接使用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']
API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'CREDENTIALS.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='2017-01-01',
endDate='2017-12-31',
metrics='estimatedMinutesWatched,views,likes,subscribersGained',
dimensions='day',
sort='day'
)
就像前面提到的,我在谷歌控制台的json文件+中有http://localhost:8888,http://localhost:8888/oauth2callback等.
‘urn:ietf:wg:OAuth:2.0:oob’在任何时候都不在我的代码/文件/Google控制台中.我还在我的OAuth同意屏幕设置中定义了.../auth/yt-analytics.readonly.
我真的不知道我错过了什么.