Python 在从youtube api复制代码后,我不确定在哪里继续
我目前正在运行本指南以开始使用youtube api:在第2步中,我复制了他们的示例代码,说明中说复制我的api密钥并替换示例代码中的YOUR_api_密钥字符串。但是,示例代码中没有您的API密钥 这是他们提供的示例代码,我找不到api_key部分。我已经有了client_secret.json文件,但当我替换client_secrets_文件时,它仍然不会执行Python 在从youtube api复制代码后,我不确定在哪里继续,python,youtube-data-api,Python,Youtube Data Api,我目前正在运行本指南以开始使用youtube api:在第2步中,我复制了他们的示例代码,说明中说复制我的api密钥并替换示例代码中的YOUR_api_密钥字符串。但是,示例代码中没有您的API密钥 这是他们提供的示例代码,我找不到api_key部分。我已经有了client_secret.json文件,但当我替换client_secrets_文件时,它仍然不会执行 # -*- coding: utf-8 -*- # Sample Python code for youtube.search.l
# -*- 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()
《快速入门指南》似乎不是最新的。它说应该有一个“凭证”下拉列表,您应该在其中选择“API密钥”以获取使用API密钥的代码示例,但没有这样的下拉列表 相反,在获得示例代码的地方,需要取消选中“GoogleOAuth2.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密钥。您跳过了步骤1吗?Google不擅长保持这些内容的一致性和最新性。@user2357112supportsMonica是真的,除了没有最新信息之外,在他们的API上找到正确的相关信息简直是一个迷宫。只是忙着用“新”和炫目来贬低“旧”产品。