-
Notifications
You must be signed in to change notification settings - Fork 713
Expand file tree
/
Copy pathcredentials.py
More file actions
25 lines (20 loc) · 802 Bytes
/
credentials.py
File metadata and controls
25 lines (20 loc) · 802 Bytes
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
import os
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
def get_creds():
creds = None
token_path = 'token.json'
creds_path = 'creds.json'
SCOPES = ['https://www.googleapis.com/auth/devstorage.read_only']
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
creds_path, SCOPES)
creds = flow.run_local_server(port=8091)
with open(token_path, 'w') as token:
token.write(creds.to_json())
return creds