Created
July 15, 2023 16:38
-
-
Save XronTrix10/e107e91e040df0ea1aeac017afc50e8e to your computer and use it in GitHub Desktop.
A Python Snippet To Generate Google token.pickle From credentials.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pickle | |
from google.auth.transport.requests import Request | |
from google_auth_oauthlib import flow as google_auth_flow | |
# Set the path to your client_secret.json file | |
client_secret_file = "<PATH_TO_CREDENTIALS.JSON>" | |
# Check if token.pickle file exists. If not, create one. | |
if not os.path.exists('token.pickle'): | |
creds = None | |
# The file token.pickle stores the user's access and refresh tokens, and is created automatically when the authorization flow completes for the first time. | |
if os.path.exists('token.pickle'): | |
with open('token.pickle', 'rb') as token: | |
creds = pickle.load(token) | |
# If there are no (valid) credentials available, let the user log in. | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
flow = google_auth_flow.InstalledAppFlow.from_client_secrets_file( | |
client_secret_file, ['https://www.googleapis.com/auth/drive']) | |
creds = flow.run_local_server(port=0) | |
# Save the credentials for the next run | |
with open('token.pickle', 'wb') as token: | |
pickle.dump(creds, token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
๐ข HOW TO USE
โ๏ธ Prerequisites :
๐ Run These Below commands in Terminal
pip install google-auth
pip install google-auth-oauthlib
pip install google-auth-httplib2
โ๏ธ Manage The Code
<PATH_TO_CREDENTIALS.JSON>
with the path to your credentials.json file downloaded from google cloud console. [ It's on line 7 ]๐ Run The Code