401 Unauthorized response using authlib AsyncOAuth2Client

285 Views Asked by At

I'm trying to use the AsyncOAuth2Client client to authenticate requests:

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

and then subsequently use the client to make a request:


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

however, when calling the above method I keep getting a 401 back: <Response [401 Unauthorized]>, despite the fact that when I look at the expires_at on the token there is still plenty of time left for it:

{'access_token': 'some_token',
 'expires_in': 3600,
 'token_type': 'Bearer',
 'expires_at': 1600388963} # now + 1hr utc

It may be relevant that I initially tried this same method with success yesterday, setting the timeout=None and it worked. However, when trying the same code today I hit these failures.

0

There are 0 best solutions below