我目前正在阅读本指南,以开始使用youtube api:https://developers.google.com/youtube/v3/quickstart/python在复制他们的示例代码的第2步中,该说明说要复制我的api密钥,并在示例代码中替换YOUR_API_KEY字符串。但是示例代码中没有YOUR_API_KEY
这里是他们提供的示例代码,我找不到api_key部分。我已经有了我的client_secret.json文件,但是当我替换client_secrets_file时,它仍然不会执行
# -*- coding: utf-8 -*-
# Sample Python code for youtube.search.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.force-ssl"]
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.search().list(
part="snippet",
maxResults=25,
q="surfing"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
快速入门指南似乎不是最新的。它说应该有一个“ Credentials”下拉菜单,您应该选择“ API key”来获取使用API密钥的代码示例,但没有这种下拉菜单。
相反,在获得示例代码的地方,您需要取消选中“ Google OAuth 2.0”框。 (我还必须编辑和取消编辑“ part”字段,以刷新代码。)这将产生以下不同的示例代码,其具有预期的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 googleapiclient.discovery
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"
DEVELOPER_KEY = "YOUR_API_KEY"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
request = youtube.channels().list(
part="snippet,contentDetails,statistics",
id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
然后您可以填写自己的API密钥。