Search code examples
google-apigoogle-oauthgoogle-api-python-client

oauth2client.client - Received token response with no refresh_token. Consider reauthenticating with prompt='consent'


I received google auth code from mobile app and use python oauth2client to exchange to access token and refresh token as follow:

credentials = client.credentials_from_clientsecrets_and_code(
                        app.config.get('GG_APP_SECRET'),
                        ['profile'],
                        authCodeFromMobileApp,
                        redirect_uri='http://example.com')

Then I received:

Received token response with no refresh_token. Consider reauthenticating with prompt='consent'.

Based on this it said that I have to set: access_type=offline But I'm not really sure where/how in oauth2client to set this?

Anyone here ever solve this issue before?

Addition:

I have tried below as well:

flow = client.flow_from_clientsecrets(app.config.get('GG_APP_SECRET'),
                                      ['profile'],
                                      message=None,
                                      cache=None,
                                      redirect_uri='http://example.com',
                                      device_uri=None)
flow.params['access_type'] = 'offline'
credentials = flow.step2_exchange(req_gg_code, http=None)

But still having the same unexpected result...


Solution

  • flow_from_clientsecrets method has parameter called prompt which you can pass a value "consent"

    flow = client.flow_from_clientsecrets(
                app.config.get('GG_APP_SECRET'),
                ['profile'],
                message=None,
                cache=None,
                redirect_uri='http://example.com',
                device_uri=None,
                prompt='consent'
    )
    

    Now, it will ask for Users consent every time, and you would get a refresh_token every time to use.