This part of the documentation covers the interface of Authlib Client.
authlib.client.
OAuth1Session
(client_id, client_secret=None, token=None, token_secret=None, redirect_uri=None, rsa_key=None, verifier=None, signature_method='HMAC-SHA1', signature_type='HEADER', force_include_body=False, **kwargs)¶Create an authorization URL by appending request_token and optional kwargs to url.
This is the second step in the OAuth 1 workflow. The user should be redirected to this authorization URL, grant access to you, and then be redirected back to you. The redirection back can either be specified during client registration or by supplying a callback URI per request.
Parameters: |
|
---|---|
Returns: | The authorization URL with new parameters embedded. |
fetch_access_token
(url, verifier=None, **kwargs)¶Method for fetching an access token from the token endpoint.
This is the final step in the OAuth 1 workflow. An access token is obtained using all previously obtained credentials, including the verifier from the authorization step.
Parameters: |
|
---|---|
Returns: | A token dict. |
fetch_request_token
(url, realm=None, **kwargs)¶Method for fetching an access token from the token endpoint.
This is the first step in the OAuth 1 workflow. A request token is obtained by making a signed post request to url. The token is then parsed from the application/x-www-form-urlencoded response and ready to be used to construct an authorization url.
Parameters: |
|
---|---|
Returns: | A Request Token dict. |
Note, realm
can also be configured when session created:
session = OAuth1Session(client_id, client_secret, ..., realm='')
Extract parameters from the post authorization redirect response URL.
Parameters: | url – The full URL that resulted from the user being redirected back from the OAuth provider to you, the client. |
---|---|
Returns: | A dict of parameters extracted from the URL. |
authlib.client.
OAuth1Auth
(client_id, client_secret=None, token=None, token_secret=None, redirect_uri=None, rsa_key=None, verifier=None, signature_method='HMAC-SHA1', signature_type='HEADER', realm=None, force_include_body=False)¶Signs the request using OAuth 1 (RFC5849)
authlib.client.
OAuth2Session
(client_id=None, client_secret=None, token_endpoint_auth_method=None, refresh_token_url=None, refresh_token_params=None, scope=None, redirect_uri=None, token=None, token_placement='header', state=None, token_updater=None, **kwargs)¶Construct a new OAuth 2 client requests session.
Parameters: |
|
---|
Generate an authorization URL and state.
Parameters: |
|
---|---|
Returns: | authorization_url, state |
fetch_access_token
(url=None, **kwargs)¶Alias for fetch_token.
fetch_token
(url=None, code=None, authorization_response=None, body='', auth=None, username=None, password=None, method='POST', headers=None, **kwargs)¶Generic method for fetching an access token from the token endpoint.
Parameters: |
|
---|---|
Returns: | A |
refresh_token
(url=None, refresh_token=None, body='', auth=None, headers=None, **kwargs)¶Fetch a new access token using a refresh token.
Parameters: |
|
---|---|
Returns: | A |
register_client_auth_method
(func)¶Extend client authenticate for token endpoint.
Parameters: | func – a function to sign the request |
---|
register_compliance_hook
(hook_type, hook)¶Register a hook for request/response tweaking.
Available hooks are:
revoke_token
(url, token, token_type_hint=None, body=None, auth=None, headers=None, **kwargs)¶Revoke token method defined via RFC7009.
Parameters: |
|
---|---|
Returns: | A |
authlib.client.
AssertionSession
(token_url, issuer, subject, audience, grant_type, claims=None, token_placement='header', scope=None, **kwargs)¶Constructs a new Assertion Framework for OAuth 2.0 Authorization Grants per RFC7521.
refresh_token
()¶Using Assertions as Authorization Grants to refresh token as described in Section 4.1.
request
(method, url, data=None, headers=None, withhold_token=False, auth=None, **kwargs)¶Send request with auto refresh token feature.
authlib.client.
OAuth2Auth
(token, token_placement='header', client=None)¶Sign requests for OAuth 2.0, currently only bearer token is supported.
authlib.client.
OAuthClient
(client_id=None, client_secret=None, request_token_url=None, request_token_params=None, access_token_url=None, access_token_params=None, refresh_token_url=None, refresh_token_params=None, authorize_url=None, authorize_params=None, api_base_url=None, client_kwargs=None, server_metadata_url=None, compliance_fix=None, **kwargs)¶A mixed OAuth client for OAuth 1 and OAuth 2.
Parameters: |
|
---|
Create an instance of OAuthClient. If request_token_url
is configured,
it would be an OAuth 1 instance, otherwise it is OAuth 2 instance:
oauth1_client = OAuthClient(
client_id='Twitter Consumer Key',
client_secret='Twitter Consumer Secret',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authenticate',
api_base_url='https://api.twitter.com/1.1/',
)
oauth2_client = OAuthClient(
client_id='GitHub Client ID',
client_secret='GitHub Client Secret',
api_base_url='https://api.github.com/',
access_token_url='https://github.com/login/oauth/access_token',
authorize_url='https://github.com/login/oauth/authorize',
client_kwargs={'scope': 'user:email'},
)
Generate the authorization url and state for HTTP redirect.
Parameters: |
|
---|---|
Returns: | (url, state) |
fetch_access_token
(redirect_uri=None, request_token=None, **params)¶Fetch access token in one step.
Parameters: |
|
---|---|
Returns: | A token dict. |
get
(url, **kwargs)¶Invoke GET http request.
If api_base_url
configured, shortcut is available:
client.get('users/lepture')
post
(url, **kwargs)¶Invoke POST http request.
If api_base_url
configured, shortcut is available:
client.post('timeline', json={'text': 'Hi'})
patch
(url, **kwargs)¶Invoke PATCH http request.
If api_base_url
configured, shortcut is available:
client.patch('profile', json={'name': 'Hsiaoming Yang'})
put
(url, **kwargs)¶Invoke PUT http request.
If api_base_url
configured, shortcut is available:
client.put('profile', json={'name': 'Hsiaoming Yang'})
delete
(url, **kwargs)¶Invoke DELETE http request.
If api_base_url
configured, shortcut is available:
client.delete('posts/123')
authlib.flask.client.
OAuth
(app=None, cache=None, fetch_token=None, update_token=None)¶Registry for oauth clients.
Parameters: | app – the app instance of Flask |
---|
Create an instance with Flask:
oauth = OAuth(app, cache=cache)
You can also pass the instance of Flask later:
oauth = OAuth()
oauth.init_app(app, cache=cache)
Parameters: |
|
---|
init_app
(app, cache=None, fetch_token=None, update_token=None)¶Init app with Flask instance.
register
(name, overwrite=False, **kwargs)¶Registers a new remote application.
Parameters: |
|
---|
Find parameters from OAuthClient
.
When a remote app is registered, it can be accessed with
named attribute:
oauth.register('twitter', client_id='', ...)
oauth.twitter.get('timeline')
authlib.flask.client.
RemoteApp
(name, fetch_token=None, update_token=None, fetch_request_token=None, save_request_token=None, **kwargs)¶Flask integrated RemoteApp of OAuthClient
.
It has built-in hooks for OAuthClient. The only required configuration
is token model.
Authorize access token.
Create a HTTP Redirect for Authorization Endpoint.
Parameters: |
|
---|---|
Returns: | A HTTP redirect response. |
Save redirect_uri
and state
into session during
authorize step.
authlib.django.client.
OAuth
(fetch_token=None)¶Registry for oauth clients.
Create an instance for registry:
oauth = OAuth()
register
(name, overwrite=False, **kwargs)¶Registers a new remote application.
Parameters: |
|
---|
Find parameters from OAuthClient
.
When a remote app is registered, it can be accessed with
named attribute:
oauth.register('twitter', client_id='', ...)
oauth.twitter.get('timeline')
authlib.django.client.
RemoteApp
(name, fetch_token=None, **kwargs)¶Django integrated RemoteApp of OAuthClient
.
It has built-in hooks for OAuthClient.
Fetch access token in one step.
Parameters: | request – HTTP request instance from Django view. |
---|---|
Returns: | A token dict. |
Create a HTTP Redirect for Authorization Endpoint.
Parameters: |
|
---|---|
Returns: | A HTTP redirect response. |
Save redirect_uri
and state
into session during
authorize step.