Skip to main content

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:

Download the notebook into JupyterLab terminal:

wget https://github.com/tire2e/notebooks/raw/main/notebooks/tir-tutorials/drive_access_in_notebook.ipynb

Prerequisites


Setup Instructions

  1. Open the Google Cloud Console and choose (or create) a project.
  2. Go to APIs & Services → OAuth consent screen.
  3. Select User type = External (unless you have an organization-internal use case).
  4. Set Publishing status = In Production to avoid 7‑day token expiry for testing credentials.
  5. 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)

  1. In Cloud Console go to APIs & Services → Credentials.

  2. Click Create credentials → OAuth client ID. If prompted, complete the consent screen first.

  3. For Application type, choose Web application.

  4. Give the credential a name (e.g., "Instance Client").

  5. Under Authorized JavaScript origins add:

    • https://notebooks.e2enetworks.com:443
  6. Under Authorized redirect URIs add:

    • https://tir.e2enetworks.com/oauth2callback/
  7. Click Create. Download the JSON file of the OAuth client (it contains client_id and client_secret).

  8. 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)
info

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_path points 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.json to the notebook environment.

References