Path: blob/master/generate_drive_token.py
1630 views
import os1import pickle2from google.auth.transport.requests import Request3from google_auth_oauthlib.flow import InstalledAppFlow45credentials = None6__G_DRIVE_TOKEN_FILE = "token.pickle"7__OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"]8if os.path.exists(__G_DRIVE_TOKEN_FILE):9with open(__G_DRIVE_TOKEN_FILE, "rb") as f:10credentials = pickle.load(f)11if (12(credentials is None or not credentials.valid)13and credentials14and credentials.expired15and credentials.refresh_token16):17credentials.refresh(Request())18else:19flow = InstalledAppFlow.from_client_secrets_file("credentials.json", __OAUTH_SCOPE)20credentials = flow.run_local_server(port=0, open_browser=False)2122# Save the credentials for the next run23with open(__G_DRIVE_TOKEN_FILE, "wb") as token:24pickle.dump(credentials, token)252627