Access Google Drive in Instances
This guide explains how to connect Google Drive with Instances. It covers setting up OAuth2 credentials, authenticating, and performing a common action (downloading a file). Other operations (search, upload, create folder) are possible and linked at the end.
Quick Start
You can use the tutorial notebook as a hands‑on example:
- Online notebook: https://tir.e2enetworks.com/github/tire2e/notebooks/blob/main/notebooks/tir-tutorials/drive_access_in_notebook.ipynb
Download the notebook into JupyterLab terminal:
wget https://github.com/tire2e/notebooks/raw/main/notebooks/tir-tutorials/drive_access_in_notebook.ipynb
Prerequisites
- Google account and access to Google Cloud Console: https://console.cloud.google.com
- Google Drive API enabled for your project: https://console.cloud.google.com/apis/api/drive.googleapis.com/
Setup Instructions
Step 1 — Configure the OAuth Consent Screen
- Open the Google Cloud Console and choose (or create) a project.
- Go to APIs & Services → OAuth consent screen.
- Select User type = External (unless you have an organization-internal use case).
- Set Publishing status = In Production to avoid 7‑day token expiry for testing credentials.
- Fill required fields (App name, Support email). Save the consent configuration.
Note: If the app is not published to In Production, user-consent tokens created via the OAuth flow will expire after 7 days and require re-authentication.
Step 2 — Create OAuth2 Credentials (detailed)
-
In Cloud Console go to APIs & Services → Credentials.
-
Click Create credentials → OAuth client ID. If prompted, complete the consent screen first.
-
For Application type, choose Web application.
-
Give the credential a name (e.g., "Instance Client").
-
Under Authorized JavaScript origins add:
https://notebooks.e2enetworks.com:443
-
Under Authorized redirect URIs add:
https://tir.e2enetworks.com/oauth2callback/
-
Click Create. Download the JSON file of the OAuth client (it contains client_id and client_secret).
-
Upload the downloaded JSON file into your JupyterLab session (use the Upload button in the file browser or the toolbar upload icon).
In your notebook, set the path to the uploaded credentials JSON, for example:
creds_file_path = "path/to/your/credentials_file.json"
Step 3 — Install required Python libraries
Run in your Instance environment:
pip3 install --upgrade google-auth google-api-python-client google-auth-oauthlib google-auth-httplib2
(Adding google-auth-httplib2 can help in some environments.)
Step 4 — Authenticate and initialize the Drive client
The code below does a robust check: it loads existing token.json if present, refreshes if possible, or runs a full OAuth flow and saves token.json for reuse.
import os
import io
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/drive']
creds = None
# Load existing credentials if available
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no valid credentials, start OAuth flow
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
creds_file_path,
SCOPES,
redirect_uri='https://tir.e2enetworks.com/oauth2callback/'
)
auth_url, _ = flow.authorization_url(prompt='consent')
print('Go to the following URL in your browser:')
print(auth_url)
code = input('Enter the authorization code: ')
flow.fetch_token(code=code)
creds = flow.credentials
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
# Build the Drive API client
service = build('drive', 'v3', credentials=creds)
How the flow works (short): the script prints a URL — open it in a browser, authorize the app with your Google account, copy the authorization code shown, and paste it back into the notebook prompt. On success, token.json is saved so you won't need to re-authenticate each run (unless the token expires).
Example (most useful): Download a file
This is the most common operation for notebook users who want to fetch datasets from Drive.
from googleapiclient.http import MediaIoBaseDownload
file_id = 'your_file_id_here' # replace with the Drive file ID
file_path = 'downloaded_file.ext' # local path where the file will be saved
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(file_path, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
if status:
print(f"Download {int(status.progress() * 100)}%.")
print('Download complete:', file_path)
The Drive API supports searching, listing, uploading, and creating folders. If you need examples for those operations, the tutorial notebook and Google Drive API docs have full samples.
Troubleshooting and best practices
- Redirect URI mismatch: The redirect URI in your OAuth credentials must match exactly
https://tir.e2enetworks.com/oauth2callback/. If it differs, the OAuth flow will fail. - Token expiry: If the OAuth consent screen is not published to In Production, tokens granted may expire after 7 days — publish to In Production for persistent testing credentials.
- Credentials path: Ensure the JSON credentials file is uploaded and
creds_file_pathpoints to the correct location in the notebook. - Authorization code: Copy the authorization code exactly (no extra spaces/newlines).
- Headless environments: If you cannot open the browser on the machine, run the flow on your local laptop, then copy
token.jsonto the notebook environment.
References
- Google Drive API guides: https://developers.google.com/drive/api/guides/about-sdk
- OAuth consent screen: https://developers.google.com/workspace/guides/configure-oauth-consent#configure_oauth_consent
- Create credentials: https://developers.google.com/workspace/guides/create-credentials#oauth-client-id