-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange_code_to_credential.py
35 lines (31 loc) · 1.39 KB
/
exchange_code_to_credential.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from google_auth_oauthlib.flow import InstalledAppFlow
import google.oauth2.credentials
google_client_secret_json = {
"web": {
"client_id": 'YOUR_GOOGLE_OAUTH_CLIENT_ID'),
"project_id": 'YOUR_GOOGLE_OAUTH_PROJECT_ID'),
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": 'YOUR_GOOGLE_OAUTH_CLIENT_SECRET'),
"redirect_uris": 'YOUR_GOOGLE_OAUTH_REDIRECT_URL')
}}
def oauth_google_exchange_code_save_credential(code, state):
try:
flow = InstalledAppFlow.from_client_config(
google_client_secret_json,
scopes=config('GOOGLE_OAUTH_SCOPE', cast=Csv()),
redirect_uri=config('GOOGLE_OAUTH_REDIRECT_URL', cast=Csv())[0]
)
credentials = flow.fetch_token(code=code)
credential = google.oauth2.credentials.Credentials(
credentials.get('access_token'),
refresh_token=credentials.get('refresh_token'),
token_uri='https://accounts.google.com/o/oauth2/token',
client_id=config('GOOGLE_OAUTH_CLIENT_ID'),
client_secret=config('GOOGLE_OAUTH_CLIENT_SECRET')
)
decoded_state = base64.b64decode(state.encode('utf-8')).decode('utf-8')
return credentials, decoded_state
except Exception as e:
return 'code_invalid', False