Google Drive OAuth2 redirect URI mismatch error in browser app

I’m building a client-side app that needs to authenticate with Google Drive using OAuth2. I keep getting an error about redirect URI not matching what’s registered.

Here’s my OAuth configuration:

Web Application Credentials
Client ID:          123456789012.apps.googleusercontent.com
Email:              [email protected]
Client secret:      [hidden]
Redirect URIs:      https://myapp.example.com/auth/
                    https://staging.myapp.com/auth/
JavaScript origins: https://myapp.example.com
                    https://staging.myapp.com

Drive SDK Credentials  
Client ID:          123456789012-abcdef123456789.apps.googleusercontent.com
Client secret:      [hidden]

When the user needs to login, I redirect them to:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&response_type=token&redirect_uri=https://staging.myapp.com/auth/&client_id=123456789012-abcdef123456789.apps.googleusercontent.com

But Google returns this error:

The redirect URI in the request: https://staging.myapp.com/auth/ did not match a registered redirect URI

scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/drive.file
response_type=token
redirect_uri=https://staging.myapp.com/auth/
client_id=123456789012-abcdef123456789.apps.googleusercontent.com

What could be causing this mismatch? The URLs look identical to me.

Had this exact problem last month - spent hours pulling my hair out! You’ve got two different OAuth2 credentials but you’re mixing them up. Your redirect URI is registered under the “Web Application Credentials” client ID (123456789012.apps.googleusercontent.com), but your auth request uses the “Drive SDK Credentials” client ID (123456789012-abcdef123456789.apps.googleusercontent.com). Google’s looking for redirect URIs under the Drive SDK client but can’t find any. Fix: either add the redirect URIs to your Drive SDK credentials or switch to the Web Application client ID in your auth URL. I’d just consolidate everything under one set of credentials so this doesn’t happen again.

hey, i think your client ID in the redirect is off. the URL is using the Drive SDK ID but the registered URIs are for the Web App. make sure they align!

You’ve got mismatched client credentials. Your auth URL is using the Drive SDK client ID (123456789012-abcdef123456789.apps.googleusercontent.com), but your redirect URIs are set up for the Web Application client ID (123456789012.apps.googleusercontent.com). Google won’t let this through since it checks that the redirect URIs match the client ID in your request. I’ve hit this same issue before - fixed it by making sure the redirect URIs were configured for the right client. You can either add the redirect URIs to your Drive SDK credentials in Google Console, or switch your auth URL to use the Web Application client ID instead.