Redirect URI mismatch error from Google OAuth

3.1k Views Asked by At

I have a Flask web application which is hosting in Google Cloud Run which is hosted with https://mydomain.run.app.

Now I am trying to add google authentication to it. I have created the API under credentials in GCP. I have given https://mydomain.run.app/authorize in the redirect uri but when I tried to login from my app it throws me redirect mismatch error. And the error shows me http://mydomain.run.app/authorize. The mismatch is the https and http When I tried to give http in the credentials uri it throws me

Invalid Redirect: This app has a publishing status of "In production". URI must use https:// as the scheme.

@app.route('/login/google')
def google_login():
    google = oauth.create_client('google')
    redirect_uri = url_for('authorize', _external=True,_scheme='https')
    return google.authorize_redirect(redirect_uri)

@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')  
    token = google.authorize_access_token()  
    resp = google.get('userinfo')  
    user_info = resp.json()
    user = oauth.google.userinfo() 
    session['profile'] = user_info
    session.permanent = True  
    return redirect('/select')
2

There are 2 best solutions below

3
On

your app is currently set to production in google developer console.

enter image description here

This means that all of the redirect uris you try to add to your project. Must be HTTPS and not HTTP you can also not use localhost

As you are trying to use http://mydomain.run.app/authorize you need to change it so that it is https://mydomain.run.app/authorize note that the first one was http:// and not https://

The error is coming because your application itself is trying to send a redirect uri of http and not https. You need to fix your application so that it is using https.

4
On

Under Authorized redirect URIs

You should put 1 more URI :

https://mydomain.run.app/

Then check again. I have got same issue before.