我正在遵循以下说明: https://developers.google.com/youtube/v3/quickstart/python
我在 Google Cloud API 和服务中创建了一个 API 密钥(Api 密钥应用程序限制设置为 NONE,API 限制设置为 DONT RESTRICT KEY。我还创建了一个 OAuth 2.0 客户端 ID。两者都在同一个项目下,其中有从 API 库启用 YouTube 数据 API v3。
当我按照说明(上面的链接)操作时:
在您下载的示例中,找到 YOUR_API_KEY 字符串并将其替换为您在本快速入门的步骤 1 中创建的 API 密钥。
如您所见,代码中不存在该文本(粘贴在下面)。
我做错了什么!!这是我第一次尝试做这样的事情,所以可能是非常明显的事情,但我找不到解决方案。
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/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_API_CODE,我无法继续
来自:https://developers.google.com/youtube/v3/quickstart/python
复制代码示例并将其保存在名为 example.py 的文件中。
在您下载的示例中,找到 YOUR_API_KEY 字符串并将其替换为您在本快速入门的步骤 1 中创建的 API 密钥。
从命令行运行示例。在您的工作目录中,运行:
python 示例.py
示例应执行请求并将响应打印到 STDOUT。
Api 密钥用于访问公共数据,因此如果您进行视频搜索,则不需要授权,因为您正在访问公共数据。它们的要点是将您的应用程序识别为提出请求的应用程序。
频道列表等方法访问用户的频道,并且需要 oauth 同意才能访问它。
如果您使用的是 oauth,则不需要 API 密钥,因为您已经使用客户端 ud 识别了您的应用程序。
我想我所说的是忽略 API 密钥,我将向 google 报告该问题并修复教程。感谢您的举报