我尝试在 python 中使用 OAuth2WebServerFlow 获取访问令牌和刷新令牌。但它抛出错误 oauth2client.client.FlowExchangeError: invalid_grantMalformed auth code。
示例代码
from oauth2client.client import OAuth2WebServerFlow
def retrieve_data():
"""
Run through the OAuth flow and retrieve credentials.
Returns a dataset (Users.dataSources.datasets):
https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets
"""
CLIENT_ID = 'XXXXX'
CLIENT_SECRET = 'XXXX'
OAUTH_SCOPE = ['https://www.googleapis.com/auth/userinfo.email']
REDIRECT_URI = 'XXXXXXX'
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print('Go to the following link in your browser:')
print(authorize_url)
code = input('Enter verification code: ')
print(code)
google_data = flow.step2_exchange(code)
# # http = httplib2.Http()
# # http = google_data.authorize(http)
# print(google_data)
# access_token = google_data.token_response['access_token']
# print(access_token)
retrieve_data()
我也有授权码。我试图在 step2_exchange 方法中传递代码。它抛出了这个错误('oauth2client.client.FlowExchangeError: invalid_grantMalformed auth code.')如何解决这个问题。