-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredential_handler.py
More file actions
41 lines (35 loc) · 1.22 KB
/
credential_handler.py
File metadata and controls
41 lines (35 loc) · 1.22 KB
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
36
37
38
39
40
41
import os.path
import sys
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPES = [
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/docs",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.metadata.readonly"
]
def request_creds():
"""
Get user credentials with google app credentials.
"""
creds = None
if os.path.exists("creds.json"):
flow = InstalledAppFlow.from_client_secrets_file("creds.json", SCOPES)
creds = flow.run_local_server(por=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return Credentials.from_authorized_user_file("token.json", SCOPES)
else:
print('Credentials not found.')
sys.exit(1)
def get_creds():
"""
Get existing user creds or prompt new auth flow.
"""
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
return creds
return request_creds()