authentication.rst
This file
Path
docs/user/authentication.rst
128 lines · 4.2 KiB
Last changed by
Merge pull request #2945 from ibnIrshad/fixdocs-2062Cory Benfieldbefore adoptiona1c9b84
History of this fileTake it
The walk stopped after 200 commits: lines older than 931fd0e point at it.
Merge pull request #2814 from Lukasa/issue/2813Ian Cordascobefore adoption931fd0e
lines 1–39why, one step back1.. _authentication:23Authentication4==============56This document discusses using various kinds of authentication with Requests.78Many web services require authentication, and there are many different types.9Below, we outline various forms of authentication available in Requests, from10the simple to the complex.111213Basic Authentication14--------------------1516Many web services that require authentication accept HTTP Basic Auth. This is17the simplest kind, and Requests supports it straight out of the box.1819Making requests with HTTP Basic Auth is very simple::2021 >>> from requests.auth import HTTPBasicAuth22 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))23 <Response [200]>2425In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand26for using it::2728 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))29 <Response [200]>3031Providing the credentials in a tuple like this is exactly the same as the32``HTTPBasicAuth`` example above.333435netrc Authentication36~~~~~~~~~~~~~~~~~~~~3738If no authentication method is given with the ``auth`` argument, Requests will39attempt to get the authentication credentials for the URL's hostname from theMerge pull request #2945 from ibnIrshad/fixdocs-2062Cory Benfieldbefore adoptiona1c9b84
lines 40–41why, one step back40user's netrc file. The netrc file overrides raw HTTP authentication headers41set with `headers=`.Merge pull request #2814 from Lukasa/issue/2813Ian Cordascobefore adoption931fd0e
lines 42–128why, one step back4243If credentials for the hostname are found, the request is sent with HTTP Basic44Auth.454647Digest Authentication48---------------------4950Another very popular form of HTTP Authentication is Digest Authentication,51and Requests supports this out of the box as well::5253 >>> from requests.auth import HTTPDigestAuth54 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'55 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))56 <Response [200]>575859OAuth 1 Authentication60----------------------6162A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``63library allows Requests users to easily make OAuth authenticated requests::6465 >>> import requests66 >>> from requests_oauthlib import OAuth16768 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'69 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',70 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')7172 >>> requests.get(url, auth=auth)73 <Response [200]>7475For more information on how to OAuth flow works, please see the official `OAuth`_ website.76For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_77repository on GitHub787980Other Authentication81--------------------8283Requests is designed to allow other forms of authentication to be easily and84quickly plugged in. Members of the open-source community frequently write85authentication handlers for more complicated or less commonly-used forms of86authentication. Some of the best have been brought together under the87`Requests organization`_, including:8889- Kerberos_90- NTLM_9192If you want to use any of these forms of authentication, go straight to their93GitHub page and follow the instructions.949596New Forms of Authentication97---------------------------9899If you can't find a good implementation of the form of authentication you100want, you can implement it yourself. Requests makes it easy to add your own101forms of authentication.102103To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the104``__call__()`` method::105106 >>> import requests107 >>> class MyAuth(requests.auth.AuthBase):108 ... def __call__(self, r):109 ... # Implement my authentication110 ... return r111 ...112 >>> url = 'http://httpbin.org/get'113 >>> requests.get(url, auth=MyAuth())114 <Response [200]>115116When an authentication handler is attached to a request,117it is called during request setup. The ``__call__`` method must therefore do118whatever is required to make the authentication work. Some forms of119authentication will additionally add hooks to provide further functionality.120121Further examples can be found under the `Requests organization`_ and in the122``auth.py`` file.123124.. _OAuth: http://oauth.net/125.. _requests_oauthlib: https://github.com/requests/requests-oauthlib126.. _Kerberos: https://github.com/requests/requests-kerberos127.. _NTLM: https://github.com/requests/requests-ntlm128.. _Requests organization: https://github.com/requests