Last active
March 24, 2024 04:57
-
-
Save sigma67/44600c51df498099695b8837f97d2849 to your computer and use it in GitHub Desktop.
obtaining an oauth.json using client_secret.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import google_auth_oauthlib.flow | |
from urllib.parse import urlparse, parse_qs | |
SCOPE = 'https://www.googleapis.com/auth/youtube' | |
# Use the client_secret.json file to identify the application requesting | |
# authorization. The client ID (from that file) and access scopes are required. | |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( | |
'client_secret.json', | |
scopes=[SCOPE]) | |
flow.redirect_uri = 'https://localhost/oauth2callback' | |
# Generate URL for request to Google's OAuth 2.0 server. | |
authorization_url, state = flow.authorization_url( | |
access_type='offline', | |
include_granted_scopes='true') | |
print(authorization_url) | |
redirect_uri = input("visit the URL above, authorize and paste the resulting redirect uri from your browser") | |
parsed_url = urlparse(redirect_uri) | |
qs = parse_qs(parsed_url.query) | |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( | |
'client_secret.json', | |
scopes=[SCOPE], | |
state=qs['state'].pop()) | |
flow.redirect_uri = 'https://localhost/oauth2callback' | |
flow.fetch_token(authorization_response=redirect_uri) | |
# Store the credentials in a JSON | |
credentials = flow.credentials | |
oauth_json = { | |
'access_token': credentials.token, | |
'refresh_token': credentials.refresh_token, | |
'token_uri': credentials.token_uri, | |
'client_id': credentials.client_id, | |
'client_secret': credentials.client_secret, | |
'scopes': credentials.scopes | |
} | |
with open('oauth.json', mode="w") as file: | |
json.dump(oauth_json, file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment