2021-11-23 09:35:16 +08:00
|
|
|
import os
|
2024-01-13 23:00:50 +08:00
|
|
|
import pickle
|
2021-11-23 09:35:16 +08:00
|
|
|
from google.auth.transport.requests import Request
|
2024-01-13 23:00:50 +08:00
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
2021-11-23 09:35:16 +08:00
|
|
|
|
|
|
|
credentials = None
|
|
|
|
__G_DRIVE_TOKEN_FILE = "token.pickle"
|
|
|
|
__OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]
|
|
|
|
if os.path.exists(__G_DRIVE_TOKEN_FILE):
|
2023-11-06 08:05:22 +08:00
|
|
|
with open(__G_DRIVE_TOKEN_FILE, "rb") as f:
|
2021-11-23 09:35:16 +08:00
|
|
|
credentials = pickle.load(f)
|
|
|
|
if (
|
2024-03-20 01:08:54 +08:00
|
|
|
(credentials is None or not credentials.valid)
|
|
|
|
and credentials
|
|
|
|
and credentials.expired
|
|
|
|
and credentials.refresh_token
|
2021-11-23 09:35:16 +08:00
|
|
|
):
|
|
|
|
credentials.refresh(Request())
|
|
|
|
else:
|
2023-11-06 08:05:22 +08:00
|
|
|
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", __OAUTH_SCOPE)
|
2022-04-18 07:28:17 +08:00
|
|
|
credentials = flow.run_local_server(port=0, open_browser=False)
|
2021-11-23 09:35:16 +08:00
|
|
|
|
|
|
|
# Save the credentials for the next run
|
2023-11-06 08:05:22 +08:00
|
|
|
with open(__G_DRIVE_TOKEN_FILE, "wb") as token:
|
2022-03-05 16:48:21 +08:00
|
|
|
pickle.dump(credentials, token)
|