skip to navigation
skip to content

alf 0.4

OAuth Client

Latest Version: 0.6.0

Python OAuth 2 Client

alf is a OAuth 2 Client based on requests.Session with seamless support for the Client Credentials Flow.


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


Initialize the client and use it as a requests.Session object.

from alf.client import Client

alf = Client(

resource_uri = ''

    resource_uri, data='{"name": "alf"}',
    headers={'Content-Type': 'application/json'})



How it works?

Before any request the client tries to retrive 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 value.

After getting the token, the request is issued with a Bearer authorization header:

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.

File Type Py Version Uploaded on Size
alf-0.4.tar.gz (md5) Source 2014-03-12 4KB
  • Downloads (All Versions):
  • 0 downloads in the last day
  • 0 downloads in the last week
  • 1093 downloads in the last month