Skip to content

Commit

Permalink
fix drive token generation and update readme
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gopale <shubham.gopale@gojek.com>
  • Loading branch information
Shubham Gopale committed May 13, 2022
1 parent 9f60efc commit 41bfd44
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Note: You can limit maximum concurrent downloads by changing the value of MAX_CO
- Visit the [Google Cloud Console](https://console.developers.google.com/apis/credentials)
- Go to the OAuth Consent tab, fill it, and save.
- Go to the Credentials tab and click Create Credentials -> OAuth Client ID
- Choose Other and Create.
- Choose `Desktop App` as Application type and Create.
- Use the download button to download your credentials.
- Move that file to the root of mirror-bot, and rename it to credentials.json
- Visit [Google API page](https://console.developers.google.com/apis/library)
Expand Down
14 changes: 9 additions & 5 deletions generate_drive_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
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:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
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_console(port=0)
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)
pickle.dump(credentials, token)

0 comments on commit 41bfd44

Please sign in to comment.