【发布时间】:2021-09-15 20:20:30
【问题描述】:
我正在尝试使用谷歌云来获取我的 youtube 频道的统计信息,但我不想每次都完成 OAuth 并将密钥输入控制台。我有一个 API 密钥,想知道它是否可以只使用 API 密钥获取统计信息,而不必通过 OAuth 流程,因为我计划将其连接到显示器并且不想经常进行 OAuth。
这是我从 Github 复制的代码,但这又一次让 OAuth 和它有点痛苦,我希望只使用 API 密钥,这样我就不必与它交互来获取我的统计信息。
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 = 'YTClientSecrets.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)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
youtubeAnalytics = get_service()
execute_api_request(
youtubeAnalytics.reports().query,
ids='channel==MINE',
startDate='2017-01-01',
endDate='2021-12-31',
metrics='estimatedMinutesWatched,views,likes,subscribersGained',
dimensions='day',
sort='day'
)
print(response.text)```
【问题讨论】:
标签: python youtube-api google-oauth google-api-python-client youtube-analytics-api