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.
答案 0 :(得分:0)
调用oauth2_flow.fetch_token(authorization_response=flask.request.url)
后,您只需在该响应中将授权码交换为访问令牌。
因此您不需要调用令牌端点,只需要获取凭据即可。
credentials = oauth2_flow.credentials
最后从credentials.token
和credentials.refresh_token
获取令牌和refresh_token。
我希望一切都清楚!