没有用户 OAuth 流程的 YouTube API [英] YouTube API without user OAuth process

查看:55
本文介绍了没有用户 OAuth 流程的 YouTube API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch captions from YouTube video using YouTube Data API (v3) https://developers.google.com/youtube/v3/guides/implementation/captions

因此,首先我尝试使用以下网址检索字幕列表:https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=KK9bwTlAvgo&key={My API KEY}

So, first I tried to retrieve a captions list using this url: https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=KK9bwTlAvgo&key={My API KEY}

我可以从上面的链接中检索我想要下载的字幕 ID (jEDP-pmNCIqoB8QGlXWQf4Rh3faalD_l).

I could retrieve the caption id that I'd like to download (jEDP-pmNCIqoB8QGlXWQf4Rh3faalD_l) from the above link.

然后,我按照此说明下载标题:https://developers.google.com/youtube/v3/docs/captions/download

Then, I followed this instruction to download the caption: https://developers.google.com/youtube/v3/docs/captions/download

然而,即使我正确输入了标题 ID 和我的 api 密钥,它也会显示需要登录"错误.

However, even though I input the caption id and my api key correctly, it shows "Login Required" error.

我想我需要 OAuth 身份验证,但我尝试做的与我的用户帐户无关,而只是自动下载公共字幕数据.

I suppose I need OAuth authentication, but what I am trying to do is not related to my users's account, but simply downloading public caption data automatically.

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

My question is: Is there any way to process OAuth authentication just once to get an access token of my own YouTube account and then reuse it whenever I need it in my application?

推荐答案

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

I can't speak to the permissions needed for the captions API in particular, but in general, yes, you can OAuth to your app once using your own account and use the access and refresh tokens to make subsequent OAuth'd requests to the API. You can find the details of generating tokens here:

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

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

To perform the steps manually (fortunately, you only need to do this once):

  1. 如果应用已获得访问权限,则需要将其删除,以便建立新的身份验证凭据.转到 https://security.google.com/settings/security/permissions(登录您的帐户时)并删除对该应用程序的访问权限.如果客户端 ID 或密码发生变化(或者您需要创建一个),请在 https://console.developers.google.com 上找到它们a> 在 API 管理器下.
  2. 要授予访问权限并接收临时代码,请在浏览器中输入此 URL:

  1. If access has already been granted for an app, it needs to be removed so that new auth credentials can be established. Go to 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 under API Manager.
  2. To grant access and receive a temporary code, enter this URL in a browser:

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

按照提示授予应用访问权限.

Follow the prompt to grant access to the app.

发送 POST 请求(例如,通过 Postman Chrome 插件)在请求正文中https://accounts.google.com/o/oauth2/token:

Send a POST request (e.g., via Postman Chrome plugin) to https://accounts.google.com/o/oauth2/token with the following in the request body:

code=<code>&
client_id=<client_id>&
client_secret=<client_secret>&
redirect_uri=http://www.google.com&
grant_type=authorization_code

  • 响应将包含访问令牌和刷新令牌.保存两者,尤其是刷新令牌(因为访问令牌将在 1 小时后过期).
  • 然后您可以使用访问令牌手动发送 OAuth 请求,遵循以下选项之一此处,本质上:

    You can then use the access token to send an OAuth'd request manually, following one of the options here, essentially:

    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
    

    (然而,当我尝试使用第二个选项作为字幕时,我收到消息:OAuth 令牌是在查询字符串中收到的,该 API 禁止 JSON 或 XML 以外的响应格式.如果可能,请尝试发送而是在 Authorization 标头中使用 OAuth 令牌.")

    (When I tried the second option for captions, however, I got the message: "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 中,这看起来像下面这样:

    You can also use the refresh token in your code to create the credential needed when building your YouTube object. In Java, this looks like the following:

    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();
    

    我想您可以使用 API 客户端库在 Python 中做类似的事情,尽管我没有没试过 Python.

    I imagine you can do something similar in Python with the API Client Libraries, although I haven't tried Python.

    这篇关于没有用户 OAuth 流程的 YouTube API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆