Auth0 Home Blog Docs

Flask-OAuth Invalid Response

python

#1

Hi,

I’m having problem to change my code to use Flask-OAuth, I implement the code that are recommended in Application Clients, but is not working

 auth0 = oauth.remote_app(
    'auth0',
    consumer_key = 'xxxxxxxxxxxxxxxxxxxx',
    consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    request_token_params={
        'scope': 'openid profile',
        'audience': 'https://' + 'engtv.auth0.com' + '/userinfo'
    },
    base_url='https://%s' % 'engtv.auth0.com',
    access_token_method='POST',
    access_token_url='/oauth/token',
    authorize_url='/authorize',
)

Callback

@auth.route("callback",methods=['POST','GET'])
def callback():
    authentication = Authentication.query.one_or_none()
    if authentication is not None:
        # Handles response from token endpoint
        resp = auth0.authorized_response()
        if resp is None:
            raise Exception('Access denied: reason=%s error=%s' % (
                request.args['error_reason'],
                request.args['error_description']
            ))
        
        url = 'https://' + authentication.auth0_domain + '/userinfo'
        headers = {'authorization': 'Bearer ' + resp['access_token']}
        resp = requests.get(url, headers=headers)
        userinfo = resp.json()
        
        # Store the tue user information in flask session.
        session[constants.JWT_PAYLOAD] = userinfo
        
        session[constants.PROFILE_KEY] = {
            'user_id': userinfo['sub'],
            'name': userinfo['name'],
            'picture': userinfo['picture']
        }
    else:
        return redirect(url_for('auth.index'))

#2

Can you give us a bit more than that, like any errors, logs, etc.?