1

I'm trying to make a chatbot in Hangouts Chat.

I'm referring this documentation to implement account linking.

Its default version is working but when I try to generate access_token and refresh token using Token Endpoint. It gives

{
    "error": "invalid_grant",
    "error_description": "Bad Request"
}

Here is my callback function code.

def on_oauth2_callback():
"""Handles the OAuth callback."""

print("IN CALLBACK ", flask.request.args)
oauth2_callback_args = OAuth2CallbackCipher.decrypt(
    flask.request.args['state'])
user_name, redirect_url = (
    oauth2_callback_args['user_name'],
    oauth2_callback_args['redirect_url'])
oauth2_flow = flow.Flow.from_client_secrets_file(
    OAUTH2_CLIENT_SECRET_FILE,
    scopes=PEOPLE_API_SCOPES,
    redirect_uri=flask.url_for('auth.on_oauth2_callback', _external=True),
    state=flask.request.args['state'])
oauth2_flow.fetch_token(authorization_response=flask.request.url)
print("REDIRECT URL ", redirect_url)

auth_code = request.args.get('code')
data = {'code': auth_code,
        'client_id': "xxxxxxxxxxxxxxx.apps.googleusercontent.com",
        'client_secret': "xxxxxxxxxxxx",
        'redirect_uri': redirect_url,
        'grant_type': 'authorization_code'}
print("%^" * 10, json.dumps(data))
r = requests.post('https://www.googleapis.com/oauth2/v3/token', data=json.dumps(data))
print("%" * 10, r.text)

return flask.redirect(redirect_url)

What am I doing wrong? And if there's another way kindly enlighten me.

1
  • What is your redirect_uri? Is this localhost or an HTTPS endpoint on your server? Commented Dec 19, 2018 at 20:40

1 Answer 1

1

Once you call oauth2_flow.fetch_token(authorization_response=flask.request.url) you just exchanged the authorization code in that response for an access token.

So you don't need to call the token endpoint, you just need to get credentials:

credentials = oauth2_flow.credentials

And finally get token and refresh_token from credentials.token and credentials.refresh_token.

Take a look on this documentation.

I hope it's clear!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.