1

I've been trying to work out how to download YouTube captions via the API but the official instructions are tailored towards command line code whereas I'm trying to do this in Python Shell.

Currently I've been following this page to no avail - https://developers.google.com/youtube/v3/docs/captions/list

What seems to trip me up is the Storage and args related pieces of code which even after much googling doesn't make any sense to me.

See the storage code below:

storage = Storage("%s-oauth2.json" % sys.argv[0]) 
#nowhere on the page does it refer to this oauth2.json file  
credentials = storage.get()
if credentials is None or credentials.invalid:    
    credentials = run_flow(flow, storage, args) 
#if credentials is none why would storage still be needed as an argument?

In the second half of the page, it's all adding arguments to args.parser which seems to be command line stuff that I don't want to use as I'm working from Python Shell.

I've also been reading the official page on getting authorization OAuth 2.0 for YouTube API so I can rewrite this code myself but I can't get past this part: https://developers.google.com/api-client-library/python/auth/web-app

Step 5: Exchange authorization code for refresh and access tokens After the web server receives the authorization code, it can exchange the authorization code for an access token. On your callback page, use the google-authlibrary to verify the authorization server response. Then, use the flow.fetch_tokenmethod to exchange the authorization code in that response for an access token:

state = flask.session['state']

this is error I'm getting from the above code statement:

RuntimeError: Working outside of request context. This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.


Essentially from lots of digging regarding the Oauth 2.0 process, I think I am unable to get the flow.fetch_token to swap my authorization code for the access token.

I've tested all my credentials so they are fine, it's getting it authorized that I'm struggling with.

This is the code so far I'm writing as a simplified version of the Captions API code on the first link:

import google.oauth2.credentials
import google_auth_oauthlib.flow
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file("//client_secret_file.json", scopes=["https://www.googleapis.com/auth/youtube.force-ssl"])
flow.redirect_uri = "http://localhost:8080"
authorization_url, state = flow.authorization_url(access_type="offline", include_granted_scopes="true")
flask.redirect(authorization_url)

[Response 929 bytes [302 FOUND]]

state = flask.session['state']

[RuntimeError]

Summary:

How can I download YouTube captions from code in Python Shell? I've set up my credentials, just can't get the web server to give me the access token so I can execute the rest of the code. I would use the code on the captions.list link but I cannot get past the Storage code statement and args.arguments which is written for command line.

SeyiA
  • 95
  • 6

1 Answers1

0

The following solution doesn't work anymore, I recommend switching to my other StackOverflow answer.

With Python 3 you can easily grab YouTube video subtitles in XML format by accessing the URL: https://video.google.com/timedtext?lang=en&v=VIDEO_ID

Here is another URL listing available subtitles available for a given YouTube video: http://video.google.com/timedtext?type=list&v=VIDEO_ID

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29895942) – isopach Sep 23 '21 at 01:39
  • I do totally agree with you. However here the links I provided are **the data** associated to the video id the OP wants to consider. There isn't any extra theory to explain here because YouTube doesn't provide any official documentation to these links which are far more easy to use than [Captions: list](https://developers.google.com/youtube/v3/docs/captions/list) and [Captions: download](https://developers.google.com/youtube/v3/docs/captions/download). The OP wants to work with YouTube data and so links can expire and OP has to accept it because he works with a not forever existing company. – Benjamin Loison Sep 23 '21 at 21:27