Skip to content
Myr

Merge pull request #1477 from dpursehouse/other-auth-basic-example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add a simple example of custom authentication in the documentation

authored by Cory Benfield · committed by Cory Benfield · parents d83919a, 62f0df4 · Browse files · 1 file+18 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+18 added−4 removed

  • other:+18 added−4 removed

1 file+18 added−4 removed

modified:docs/user/authentication.rst+18 added-4 removed
56 unmodified lines
5758OAuth 1 Authentication59----------------------6061A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib`` library allows Requests users to easily make OAuth authenticated requests::61A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``62library allows Requests users to easily make OAuth authenticated requests::6364 >>> import requests65 >>> from requests_oauthlib import OAuth166
4 unmodified lines
71 >>> requests.get(url, auth=auth)72 <Response [200]>7374For more information on how to OAuth flow works, please see the official `OAuth`_ website.74For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_ repository on GitHub75For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_76repository on GitHub777879Other Authentication80--------------------
18 unmodified lines
99want, you can implement it yourself. Requests makes it easy to add your own100forms of authentication.101102To do so, subclass :class:`requests.auth.AuthBase` and implement the101``__call__()`` method. When an authentication handler is attached to a request,103``__call__()`` method::104105 >>> import requests106 >>> class MyAuth(requests.auth.AuthBase):107 ... def __call__(self, r):108 ... # Implement my authentication109 ... return r110 ...111 >>> url = 'http://httpbin.org/get'112 >>> requests.get(url, auth=MyAuth())113 <Response [200]>114115When an authentication handler is attached to a request,116it is called during request setup. The ``__call__`` method must therefore do117whatever is required to make the authentication work. Some forms of118authentication will additionally add hooks to provide further functionality.119106Examples can be found under the `Requests organization`_ and in the120Further examples can be found under the `Requests organization`_ and in the121``auth.py`` file.122123.. _OAuth: http://oauth.net/124.. _requests_oauthlib: https://github.com/requests/requests-oauthlib
4 unmodified lines