ehsan-dashboards/dashboards/gen_oauth_url.py

60 lines
1.8 KiB
Python

import json
import urllib.parse
import secrets
import hashlib
import base64
CLIENT_SECRET_FILE = '/home/hermeswebui/.hermes/webui/attachments/00e47f151810/client_secret_950973483329-j0nrfmfdfrviohuiq85t3rc6e5e512jk.apps.googleusercontent.com.json'
with open(CLIENT_SECRET_FILE) as f:
data = json.load(f)
# Can be "installed" or "web"
client_info = data.get('installed') or data.get('web')
client_id = client_info['client_id']
client_secret = client_info['client_secret']
# Scopes needed for GSC, GA4, GTM, and userinfo
scopes = [
'openid',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/webmasters.readonly',
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/tagmanager.readonly'
]
# Generate PKCE verifier and challenge
code_verifier = secrets.token_urlsafe(64)
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode('ascii')).digest()).decode('ascii').rstrip('=')
state = secrets.token_urlsafe(16)
redirect_uri = 'http://localhost:1'
# Save pending auth state for step 2
pending_state = {
'client_id': client_id,
'client_secret': client_secret,
'code_verifier': code_verifier,
'state': state,
'redirect_uri': redirect_uri,
'scopes': scopes
}
with open('/workspace/personal/oauth_pending.json', 'w') as f:
json.dump(pending_state, f, indent=2)
params = {
'client_id': client_id,
'redirect_uri': redirect_uri,
'response_type': 'code',
'scope': ' '.join(scopes),
'access_type': 'offline',
'prompt': 'consent',
'state': state,
'code_challenge': code_challenge,
'code_challenge_method': 'S256'
}
auth_url = 'https://accounts.google.com/o/oauth2/v2/auth?' + urllib.parse.urlencode(params)
print("AUTH_URL:")
print(auth_url)