没有用户 OAuth 流程的 YouTube API

YouTube API without user OAuth process

我正在尝试使用 YouTube 数据从 YouTube 视频中获取字幕 API (v3) https://developers.google.com/youtube/v3/guides/implementation/captions

因此,首先我尝试使用此 url 检索字幕列表: https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=KK9bwTlAvgo&key={我的 API 密钥}

我可以从上面 link.

检索到我想下载的字幕 ID (jEDP-pmNCIqoB8QGlXWQf4Rh3faalD_l)

然后,我按照以下说明下载了字幕: https://developers.google.com/youtube/v3/docs/captions/download

然而,即使我正确输入了标题 ID 和我的 api 密钥,它仍显示 "Login Required" 错误。

我想我需要 OAuth 身份验证,但我想做的与我的用户帐户无关,只是自动下载 public 字幕数据。

我的问题是:有没有什么方法可以只处理一次 OAuth 身份验证以获取我自己的 YouTube 帐户的访问令牌,然后在我的应用程序中需要时重新使用它?

我无法具体说明字幕 API 所需的权限,但总的来说,是的,您可以使用自己的帐户对您的应用进行 OAuth,并使用访问和刷新令牌来向 API 发出后续 OAuth 请求。您可以在此处找到生成令牌的详细信息:

https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps#Obtaining_Access_Tokens

要手动执行这些步骤(幸运的是,您只需执行一次):

  1. 如果某个应用已被授予访问权限,则需要将其删除,以便可以建立新的身份验证凭据。转到 API 经理下的 https://security.google.com/settings/security/permissions (while logged into your account) and remove access to the app. If the client ID or secret change (or you need to create one), find them at https://console.developers.google.com
  2. 要授予访问权限并接收临时代码,请在浏览器中输入此 URL:

    https://accounts.google.com/o/oauth2/auth?
    client_id=<client_id>&
    redirect_uri=http://www.google.com&
    scope=https://www.googleapis.com/auth/youtube.force-ssl&
    response_type=code&
    access_type=offline&
    approval_prompt=force
    

    按照提示授予对应用程序的访问权限。

  3. 这将使用 code 参数重定向到 google.com(例如, https://www.google.com/?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&gws_rd=ssl#)。保存代码。
  4. 发送 POST 请求(例如,通过 Postman Chrome plugin) to https://accounts.google.com/o/oauth2/token 并在请求正文中包含以下内容:

    code=<code>&
    client_id=<client_id>&
    client_secret=<client_secret>&
    redirect_uri=http://www.google.com&
    grant_type=authorization_code
    
  5. 响应将同时包含访问令牌和刷新令牌。保存两者,尤其是刷新令牌(因为访问令牌将在 1 小时后过期)。

然后您可以使用访问令牌手动发送 OAuth 请求,遵循选项之一 here,本质上:

curl -H "Authorization: Bearer ACCESS_TOKEN" https://www.googleapis.com/youtube/v3/captions/<id>

curl https://www.googleapis.com/youtube/v3/captions/<id>?access_token=ACCESS_TOKEN

(当我尝试第二个字幕选项时,我收到消息:"The OAuth token was received in the query string, which this API forbids for response formats other than JSON or XML. If possible, try sending the OAuth token in the Authorization header instead.")

您还可以在代码中使用刷新令牌来创建构建 YouTube 对象时所需的凭据。在 Java 中,如下所示:

String clientId = <your client ID>
String clientSecret = <your client secret>
String refreshToken = <refresh token>

HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(transport)
    .setJsonFactory(jsonFactory)
    .setClientSecrets(clientId, clientSecret)
    .build()
    .setRefreshToken(refreshToken);

try {
    credential.refreshToken();
} catch (IOException e) {
    e.printStackTrace();
}

youtube = new YouTube.Builder(transport, jsonFactory, credential).build();

我想你可以在 Python 中用 API Client Libraries 做类似的事情,尽管我还没有尝试 Python。