Search code examples
pythonoauth-2.0google-drive-apigoogle-oauthgoogle-colaboratory

Google Colab - redirect_url for Google Drive API - OAuth 2.0


On Google Colab, I want to download a specific revision of a file from Google Drive to my workspace, however I found it extremely difficult (comparing how easy is to mount the drive).

I have created OAuth 2.0 Client ID, added 'expected' redirect_url's and uploaded the client_secrets.json to my workspace. And trying to download the revision with this script:

import requests
from google_auth_oauthlib.flow import InstalledAppFlow

from google.colab import auth
auth.authenticate_user()

# Set up the OAuth flow
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', scopes=['https://www.googleapis.com/auth/drive'])

# Start the OAuth authorization process
credentials = flow.run_local_server(host='localhost',
    port=8081, 
    authorization_prompt_message='Please visit this URL: {url}', 
    success_message='The auth flow is complete; you may close this window.',
    open_browser=True)

# Save the credentials to a file
credentials.save_to_disk('credentials.json')

# Make an HTTP GET request to the selfLink URL
# I was able to fetch the FILE_ID and REVISION_ID both from download link given on Google Drive's UI
# ...and on developers.google.com/drive/api/reference/rest/v3

self_link = f'https://www.googleapis.com/drive/v2/files/{FILE_ID}/revisions/{REVISION_ID}'
response = requests.get(self_link, headers={'Authorization': 'Bearer ' + credentials.token})

# Save the response content as the downloaded file
with open('your_file_name', 'wb') as file:
    file.write(response.content)

However, on flow.run_local_server step, when I visit the authentication link, I'm getting Error 400: redirect_uri_mismatch, where the request detail is: redirect_uri=http://localhost:8081/.

I have no idea how to make this work, I have waited from 5 mins to hours to let Google workspace to save my redirect url, left out the ports, tried different ports, removed http, removed trailing backslash, used postmessage in the code for host (there is no way to register postmessage in the OAuth manager as it expects a URI).

I have also tried to use the Google API like this, and in this case, I am getting file not found error:

from googleapiclient.discovery import build
drive_service = build('drive', 'v3', credentials=credentials)
files = drive_service.revisions().get_media(fileID=FILE_ID, revisionID=REVISION_ID).execute()

I have followed this suggested material as well: https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md

Anybody has an idea (or a workaround) to download a revision of a file from Drive into Google Colab workspace? P.S: As a workaround, I am not able to delete the revisions manually until the revision I want, probably due to storage constraints on Drive side. This will be a topic of another question I think.


Solution

  • in google cloud console for your project add the following as a redirect URI

    http://localhost:8081/