您可以单独使用 Python 请求查询 YouTube API v3 吗?

Can you query the YouTube API v3 with Python Requests alone?

v2 似乎是很久以前创建的,所以我专注于让 v3 满足我的需求。 v3 的 Python 代码示例都是 very long,虽然我在我的 Google 帐户中创建了一个项目和一个 API 密钥,但下载 client_secrets.json :

{
  "installed": {
    "client_id": "********",
    "project_id": "my-project",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "***********",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http://localhost"
    ]
  }
}

到目前为止,代码示例没有取得太大进展。

所以我正在测试一些更简单的东西,只是为了让 Python Requests library 拉取这个请求 -

GET https://www.googleapis.com/youtube/v3/videoCategories

在官方 v3 文档中找到 here

我将以下代码与上面 json 中的元素一起使用:

import requests
r = requests.get("https://www.googleapis.com/youtube/v3/videoCategories", auth=(client_id, client_secret))
r.status_code

这将返回 403(禁止访问),因此在此阶段无法继续。有谁知道如何让它工作,甚至可能吗?

403 (Forbidden) 表示您无权执行您想要执行的操作。您应该检查从服务器获得的完整错误消息,通常不止这些。

videoCategories.list 是一种 public 方法,从 Google 开发者控制台获取 API 密钥,然后将其添加到末尾

https://www.googleapis.com/youtube/v3/videoCategories?key=yourkey

使用官方Google python client library会让你的生活轻松很多。一旦开始使用需要 Oauth2 的方法,如果可以避免的话,您不想自己编写代码。