Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/generate_drive_token.py
1630 views
1
import os
2
import pickle
3
from google.auth.transport.requests import Request
4
from google_auth_oauthlib.flow import InstalledAppFlow
5
6
credentials = None
7
__G_DRIVE_TOKEN_FILE = "token.pickle"
8
__OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]
9
if os.path.exists(__G_DRIVE_TOKEN_FILE):
10
with open(__G_DRIVE_TOKEN_FILE, "rb") as f:
11
credentials = pickle.load(f)
12
if (
13
(credentials is None or not credentials.valid)
14
and credentials
15
and credentials.expired
16
and credentials.refresh_token
17
):
18
credentials.refresh(Request())
19
else:
20
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", __OAUTH_SCOPE)
21
credentials = flow.run_local_server(port=0, open_browser=False)
22
23
# Save the credentials for the next run
24
with open(__G_DRIVE_TOKEN_FILE, "wb") as token:
25
pickle.dump(credentials, token)
26
27