【发布时间】:2019-12-23 04:13:05
【问题描述】:
我开始研究将 YouTube API 与 python 结合使用,但文档中给出的示例似乎与快速入门指南不一致。
可以在here 找到快速入门指南,他们建议您从 API 文档here 中获取一些示例代码,这些代码将从 API 请求“YouTube 开发者”频道的一些信息。现在在快速入门指南中,他们说用您的 API 密钥替换“YOUR_API_KEY”字符串,但您可以从示例代码中看到它不存在。
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.channels().list(
part="snippet,contentDetails,statistics",
id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
我相信“YOUR_CLIENT_SECRET_FILE”字符串仅适用于需要用户身份验证的请求,这种类型的请求不应该是这种情况。
那么您应该在哪里提供 API 密钥?指南过时了吗?
非常感谢所有帮助!
【问题讨论】: