Skip to main content

OAuth Client For aiohttp

Project description

aio-alf |build-status|

aiohttp OAuth 2 Client

`aio-alf` is a OAuth 2 Client base on the aiohttp's AsyncHTTPClient


* Automatic token retrieving and renewing
* Token expiration control
* Automatic retry on status 401 (UNAUTHORIZED)


Initialize the client and use it as a AsyncHTTPClient object.

.. code-block:: python

from aioalf.client import Client
from aioalf.httpclient import HTTPRequest

client = Client(

resource_uri = ''

response = await client.request(
data='{"name": "alf"}',
headers={'Content-Type': 'application/json'}

Alternatively one can pass directly a string to the fetch client

.. code-block:: python

# ...
response = await client.request(
data='{"name": "alf"}',
headers={'Content-Type': 'application/json'}

Implicit Flow

Support for OAuth2 implict flow to enable it, call `use_implicit_flow` with a `TokenStorage`
object and a port range, it defaults to the range (32000, 32009).


.. code-block:: python

await use_implicit_flow(TokenStorage(), (30000, 30009))

async with Client(token_endpoint='https://token.endpoint',
client_secret='') as client:
response = await client.request('GET', '')
text = await response.text()

The library has a really simple in memory token storage, you should subclass and overwrite
its methods if you need to persist the token for a longer period.

How it works?

Before any request the client tries to retrieve a token on the endpoint,
expecting a JSON response with the ``access_token`` and ``expires_in`` keys.

The client keeps the token until it is expired, according to the ``expires_in``

After getting the token, the request is issued with a `Bearer authorization
header <>`_:

.. code-block::

GET /resource/1 HTTP/1.1
Authorization: Bearer token

If the request fails with a 401 (UNAUTHORIZED) status, a new token is retrieved
from the endpoint and the request is retried. This happens only once, if it
fails again the error response is returned.


In case of an error retrieving a token, the error response will be returned,
the real request won't happen.

Related projects

This project tries to be an adaptation to aiohttp of
`alf <>`_

.. |build-status| image::

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for aio-alf, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size aio-alf-0.4.0.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page