Repository navigation
Google Drive Authentication
⚠️ Breaking change (as of this update): the token generation method has changed. Anytoken.picklecreated with the old cell (viaoauth2client/GoogleCredentials.get_application_default()) is no longer compatible and must be regenerated using the steps below.
This page explains how to generate a token.pickle file that the bot
uses to authenticate with the Google Drive API, and that supports
automatic token refresh so you don't have to regenerate it every
time the access token expires.
- Go to console.cloud.google.com and create a new project (or use an existing one).
- Go to APIs & Services → Library, search for Google Drive API, and click Enable.
- Go to APIs & Services → OAuth consent screen:
- Set User type to External (unless you're using a Google Workspace organization account).
- Fill in the required app info (name, support email, etc. — this is only used internally, values don't matter much).
- Under Test users, add the Gmail address of the account whose Drive you want the bot to access.
- Go to APIs & Services → Credentials → Create Credentials → OAuth
client ID:
- Application type: Desktop app.
- Give it a name and click Create.
- Download the resulting JSON file and rename it to
client_secrets.json.
- Upload
client_secrets.jsonto your Colab session (drag-and-drop it into the file panel, into/content/).
Because the app is still in "Testing" status (not published/verified by Google), you'll see a "Google hasn't verified this app" warning during login. This is expected — click Advanced → Go to (app name) (unsafe) to continue, since you are the test user yourself.
Copy-paste the code from google_auth.py into a single cell, then run it.
DO = "Generate"-
CLIENT_SECRETS_FILEpointing to the uploadedclient_secrets.jsonpath (default/content/client_secrets.json) -
SAVE_TOset to where you wanttoken.picklesaved (default/content/token.pickle)
The cell will print an authorization URL. Follow these steps:
- Open the printed URL in your browser and sign in with the Google account whose Drive you want to use.
- Grant the requested permissions.
- Your browser will then try to load a
http://localhost/?state=...&code=...page and fail to connect. This is expected — do not close the tab. - Copy the value of the
code=parameter from that URL in your browser's address bar (everything aftercode=and before the next&). - Paste that code back into the Colab prompt when asked.
The cell will then save token.pickle and print whether a refresh
token was successfully obtained.
-
client_secrets.jsonis a secret credential — never commit it to the repository. It is per-user and is only needed locally in your Colab session. - The requested scope is
drive.readonly, matching what the bot currently does (download only, no uploads via the Drive API). If a future feature requires write access to Drive, the scope will change and all users will need to regeneratetoken.pickleagain. - Once generated,
token.picklewill be refreshed automatically by the bot when it expires, as long as the refresh token remains valid (i.e. access hasn't been revoked from your Google account and the token hasn't been unused for an extended period).