YouTube API without user OAuth process

自作多情 提交于 2019-12-05 00:11:20


I am trying to fetch captions from YouTube video using YouTube Data API (v3)

So, first I tried to retrieve a captions list using this url:{My API KEY}

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

Then, I followed this instruction to download the caption:

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

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.

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?


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:

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

  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 (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 under API Manager.
  2. To grant access and receive a temporary code, enter this URL in a browser:

    Follow the prompt to grant access to the app.

  3. This will redirect to with a code parameter (e.g., Save the code.
  4. Send a POST request (e.g., via Postman Chrome plugin) to with the following in the request body:

  5. The response will contain both an access token and refresh token. Save both, but particularly the refresh token (because the access token will expire in 1 hour).

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"<id>



(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.")

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()
    .setClientSecrets(clientId, clientSecret)

try {
} catch (IOException e) {

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

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