Score:0

Redirect URI mismatch error from Google OAuth for flask app

lu flag

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')

Then I got to know my application itself sending only http request so I tried to add _scheme='https' in the url_for while redirecting but now I am getting authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response. error. I have changed my secret key to static string instead of random numbers which I got from other answers. But not able to solve this problem yet.

Score:0
cn flag

In front of your Cloud Run service is the GFE (Google FrontEnd). Your app connects to the GFE using HTTPS or is redirected to HTTPS if it connects using HTTP. The GFE connects to your app using HTTP.

Your application thinks the client connected using HTTP, so is responding with http:// URLs. You need to process the X-Forwarded-Proto to detect the client connection method and then respond with the correct HTTPS URL. Look at ProxyFix middleware or similar.

lu flag
Thanks for the detailed explanation. But still I am getting authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response. error. Will it be related to authlib version? Once I changed the _scheme it is giving me https url only.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.