我正在尝试使用AsyncOAuth2Client客户端对请求进行身份验证:
async def get_oauth_client():
client = AsyncOAuth2Client(os.environ['OAUTH_CLIENT_ID'],
os.environ['OAUTH_CLIENT_SECRET'],
scope='my_scope',
grant_type='client_credentials',
token_endpoint=os.environ['OAUTH_TOKEN_URL'],
timeout=3600)
await client.fetch_token(os.environ['OAUTH_TOKEN_URL'])
return client
,然后使用客户端发出请求:
async def make_request(method, data, headers):
client = await get_oauth_client()
await client.ensure_active_token()
method = getattr(client, method)
response = await method(url, data=data, headers=headers)
client.close()
return response
但是,当调用上述方法时,我会一直返回401:<Response [401 Unauthorized]>
,尽管事实上,当我查看令牌上的expires_at
时,仍然还有很多时间:
{'access_token': 'some_token',
'expires_in': 3600,
'token_type': 'Bearer',
'expires_at': 1600388963} # now + 1hr utc
可能与我昨天成功尝试相同的方法(设置timeout=None
并成功)有关。但是,今天尝试相同的代码时,我遇到了这些失败。