-
Notifications
You must be signed in to change notification settings - Fork 0
/
gentok.py
30 lines (26 loc) · 1.18 KB
/
gentok.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
import pickle
import os
try:
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
except ImportError as e:
print(f"Missing import pkg: {e}")
import subprocess
print("Will installing automatically pkg")
subprocess.run(["pip3", "install", "--no-cache-dir", "google-api-python-client", "google_auth_oauthlib", "google-auth-httplib2"])
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
credentials = None
__G_DRIVE_TOKEN_FILE = "token.pickle"
__OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]
if os.path.exists(__G_DRIVE_TOKEN_FILE):
with open(__G_DRIVE_TOKEN_FILE, "rb") as f:
credentials = pickle.load(f)
if (credentials is None or not credentials.valid) and credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", __OAUTH_SCOPE)
credentials = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(__G_DRIVE_TOKEN_FILE, "wb") as token:
pickle.dump(credentials, token)