使用 Google API python 库发出请求时,您在哪里指定 API 密钥?
Where do you specify your API key when making a request with the Google API python library?
我开始考虑将 YouTube API 与 python 一起使用,但文档中给出的示例似乎与快速入门指南不一致。
可以找到快速入门指南 here and in it they recommend you grab some sample code from the API documentation here,它将向 API 请求 "YouTube Developers" 频道的一些信息。现在在快速入门指南中,他们说用 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 密钥?该指南是否已过时?
非常感谢所有帮助!
- 您想使用带有 YouTube 数据 API v3 的 API 键来检索频道列表。
- 您已经 API 使用 YouTube 数据 API v3.
- 您想使用 Python.
实现此 google-api-python-客户端
- 您已经能够使用 YouTube 数据 API v3.
如果我的理解是正确的,这个示例脚本怎么样?
示例脚本:
在使用此脚本之前,请设置您的 API 密钥。
from googleapiclient.discovery import build
api_key = "###" # Please set your API key
api_service_name = "youtube"
api_version = "v3"
youtube = build(api_service_name, api_version, developerKey=api_key)
request = youtube.channels().list(
part="snippet,contentDetails,statistics",
id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)
注:
- 此示例脚本假定您已经能够使用 YouTube 数据 API v3。如果出现API相关的错误,请确认API是否开启。
参考:
- google-api-python-client
- 特别是,我认为 here 将有助于理解如何使用 API 键。
如果我误解了您的问题而这不是您想要的结果,我深表歉意。
我从来没有得到未经授权的 API 密钥来工作,所以我最终改用了 OAuth 2.0。
从 Google API 控制台转到“凭据”面板。如果您还没有创建一个客户端 ID,然后将其下载到与您正在 运行ning.Python 脚本相同的目录。
打开您的 Python 脚本,找到 client_secrets_file 字符串。将 "YOUR_CLIENT_SECRET_FILE.json" 更改为您刚刚下载的 .json 文件的文件名。
当你的运行脚本,它会要求你在你的浏览器中打开一个link来完成授权过程。完成它,它应该 运行 如预期的那样。希望这有帮助。
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
我开始考虑将 YouTube API 与 python 一起使用,但文档中给出的示例似乎与快速入门指南不一致。
可以找到快速入门指南 here and in it they recommend you grab some sample code from the API documentation here,它将向 API 请求 "YouTube Developers" 频道的一些信息。现在在快速入门指南中,他们说用 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 密钥?该指南是否已过时?
非常感谢所有帮助!
- 您想使用带有 YouTube 数据 API v3 的 API 键来检索频道列表。
- 您已经 API 使用 YouTube 数据 API v3.
- 您想使用 Python. 实现此 google-api-python-客户端
- 您已经能够使用 YouTube 数据 API v3.
如果我的理解是正确的,这个示例脚本怎么样?
示例脚本:
在使用此脚本之前,请设置您的 API 密钥。
from googleapiclient.discovery import build
api_key = "###" # Please set your API key
api_service_name = "youtube"
api_version = "v3"
youtube = build(api_service_name, api_version, developerKey=api_key)
request = youtube.channels().list(
part="snippet,contentDetails,statistics",
id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)
注:
- 此示例脚本假定您已经能够使用 YouTube 数据 API v3。如果出现API相关的错误,请确认API是否开启。
参考:
- google-api-python-client
- 特别是,我认为 here 将有助于理解如何使用 API 键。
如果我误解了您的问题而这不是您想要的结果,我深表歉意。
我从来没有得到未经授权的 API 密钥来工作,所以我最终改用了 OAuth 2.0。
从 Google API 控制台转到“凭据”面板。如果您还没有创建一个客户端 ID,然后将其下载到与您正在 运行ning.Python 脚本相同的目录。
打开您的 Python 脚本,找到 client_secrets_file 字符串。将 "YOUR_CLIENT_SECRET_FILE.json" 更改为您刚刚下载的 .json 文件的文件名。
当你的运行脚本,它会要求你在你的浏览器中打开一个link来完成授权过程。完成它,它应该 运行 如预期的那样。希望这有帮助。
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"