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.