Error with Google Drive API: Redirect URI Mismatch

I’m trying to set up the Google Drive API for my project. I followed the quickstart guide, but I’m getting an error about a redirect URI mismatch. The error message says the requested URI doesn’t match any registered ones.

I’ve enabled the Drive API in the Google Cloud Console and set up my API access. I’ve got my client ID and secret, which I’ve added to my code. But I’m stuck on the redirect URI part.

What should I put for the redirect URI? I’m not sure what it’s supposed to be or where to find the right value. Can anyone help me figure out how to fix this mismatch error?

Here’s a simplified version of my code:

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/drive.metadata.readonly'])
service = build('drive', 'v3', credentials=creds)

results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(f"{item['name']} ({item['id']})")

Any advice would be really helpful. Thanks!

The redirect URI mismatch error is a common stumbling block when setting up Google Drive API. To resolve this, you need to configure the redirect URI in both your Google Cloud Console project and your application code. For desktop applications, you can use ‘http://localhost’ followed by a port number (e.g., ‘http://localhost:8080’). Alternatively, for increased security, Google recommends using ‘urn:ietf:wg:oauth:2.0:oob’ for installed applications.

Make sure to add this URI to the ‘Authorized redirect URIs’ section in your Google Cloud Console project settings. Then, update your code to use the same URI when initializing the OAuth 2.0 flow. This should resolve the mismatch error and allow your application to authenticate properly with the Google Drive API.

hey tom, i had this issue too! make sure ur redirect URI in the google cloud console matches the one in ur code. for local testing, try using ‘http://localhost:8080’ as the URI. also double-check if ur using the right client ID and secret. good luck man!

I’ve been through this headache before, and it can be frustrating. Here’s what worked for me:

In your Google Cloud Console, navigate to the OAuth consent screen. Make sure you’ve added yourself as a test user if you’re still in testing mode.

For the redirect URI, I found using ‘http://localhost:8080’ works well for local development. Add this to your authorized redirect URIs in the Cloud Console.

In your Python code, you’ll need to handle the OAuth flow manually. Use the google_auth_oauthlib.flow module to create a Flow object with your client secrets and the correct redirect URI.

Don’t forget to run your script with --noauth_local_webserver flag if you’re using a local server for the redirect.

Lastly, ensure your scopes match exactly between your Cloud Console settings and your code. Even a small mismatch can cause issues.

Keep at it, and you’ll get it working!