-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcredentials_plclogpoc.py
31 lines (27 loc) · 1.19 KB
/
credentials_plclogpoc.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
import pickle
import os.path
from google_auth_oauthlib.flow import InstalledAppFlow as IAF
from google.auth.transport.requests import Request
### SCOPES:
### 0) https://developers.google.com/sheets/api/guides/authorizing
### 1) Read, Write, Edit, Delete spreadsheets
def get_creds(SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
,TOKEN_FILE = 'token.pickle'
,CREDENTIAL_FILE = 'credentials.json'
):
urlpr = 'Please visit this URL to authorize this application: {url}'
codepr = 'Enter the authorization code: '
creds = False
if os.path.exists(TOKEN_FILE):
with open(TOKEN_FILE, 'rb') as ftok: creds = pickle.load(ftok)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = IAF.from_client_secrets_file(CREDENTIAL_FILE,SCOPES)
creds=flow.run_console(authorization_prompt_message=urlpr
,authorization_code_message=codepr
)
# Pickle credentials for future runs
with open(TOKEN_FILE, 'wb') as ftok: pickle.dump(creds, ftok)
return creds