authentication.rst
This file
Path
docs/user/authentication.rst
155 lines · 5.7 KiB
Last changed by
Add netrc file search information to authentication documentation (#6876)Piotr Szlazakbefore adoption59f8aa2
History of this fileTake it
The walk stopped after 200 commits: lines older than 24dfbfd point at it.
Update quickstart.rst : fixing brotlicffi link (#6044)Chavithrabefore adoption24dfbfd
lines 1–46why, 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 >>> basic = HTTPBasicAuth('user', 'pass')23 >>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)24 <Response [200]>2526In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand27for using it::2829 >>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))30 <Response [200]>3132Providing the credentials in a tuple like this is exactly the same as the33``HTTPBasicAuth`` example above.343536netrc Authentication37~~~~~~~~~~~~~~~~~~~~3839If no authentication method is given with the ``auth`` argument, Requests will40attempt to get the authentication credentials for the URL's hostname from the41user's netrc file. The netrc file overrides raw HTTP authentication headers42set with `headers=`.4344If credentials for the hostname are found, the request is sent with HTTP Basic45Auth.46Add netrc file search information to authentication documentation (#6876)Piotr Szlazakbefore adoption59f8aa2
lines 47–56why, one step back47Requests will search for the netrc file at `~/.netrc`, `~/_netrc`, or at the path48specified by the `NETRC` environment variable. `~` denotes the user's home49directory, which is `$HOME` on Unix based systems and `%USERPROFILE%` on Windows.5051Usage of netrc file can be disabled by setting `trust_env` to `False` in the52Requests session::5354 >>> s = requests.Session()55 >>> s.trust_env = False56 >>> s.get('https://httpbin.org/basic-auth/user/pass')Update quickstart.rst : fixing brotlicffi link (#6044)Chavithrabefore adoption24dfbfd
lines 57–155why, one step back5758Digest Authentication59---------------------6061Another very popular form of HTTP Authentication is Digest Authentication,62and Requests supports this out of the box as well::6364 >>> from requests.auth import HTTPDigestAuth65 >>> url = 'https://httpbin.org/digest-auth/auth/user/pass'66 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))67 <Response [200]>686970OAuth 1 Authentication71----------------------7273A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``74library allows Requests users to easily make OAuth 1 authenticated requests::7576 >>> import requests77 >>> from requests_oauthlib import OAuth17879 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'80 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',81 ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')8283 >>> requests.get(url, auth=auth)84 <Response [200]>8586For more information on how to OAuth flow works, please see the official `OAuth`_ website.87For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_88repository on GitHub8990OAuth 2 and OpenID Connect Authentication91-----------------------------------------9293The ``requests-oauthlib`` library also handles OAuth 2, the authentication mechanism94underpinning OpenID Connect. See the `requests-oauthlib OAuth2 documentation`_ for95details of the various OAuth 2 credential management flows:9697* `Web Application Flow`_98* `Mobile Application Flow`_99* `Legacy Application Flow`_100* `Backend Application Flow`_101102Other Authentication103--------------------104105Requests is designed to allow other forms of authentication to be easily and106quickly plugged in. Members of the open-source community frequently write107authentication handlers for more complicated or less commonly-used forms of108authentication. Some of the best have been brought together under the109`Requests organization`_, including:110111- Kerberos_112- NTLM_113114If you want to use any of these forms of authentication, go straight to their115GitHub page and follow the instructions.116117118New Forms of Authentication119---------------------------120121If you can't find a good implementation of the form of authentication you122want, you can implement it yourself. Requests makes it easy to add your own123forms of authentication.124125To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the126``__call__()`` method::127128 >>> import requests129 >>> class MyAuth(requests.auth.AuthBase):130 ... def __call__(self, r):131 ... # Implement my authentication132 ... return r133 ...134 >>> url = 'https://httpbin.org/get'135 >>> requests.get(url, auth=MyAuth())136 <Response [200]>137138When an authentication handler is attached to a request,139it is called during request setup. The ``__call__`` method must therefore do140whatever is required to make the authentication work. Some forms of141authentication will additionally add hooks to provide further functionality.142143Further examples can be found under the `Requests organization`_ and in the144``auth.py`` file.145146.. _OAuth: https://oauth.net/147.. _requests_oauthlib: https://github.com/requests/requests-oauthlib148.. _requests-oauthlib OAuth2 documentation: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html149.. _Web Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#web-application-flow150.. _Mobile Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#mobile-application-flow151.. _Legacy Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow152.. _Backend Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow153.. _Kerberos: https://github.com/requests/requests-kerberos154.. _NTLM: https://github.com/requests/requests-ntlm155.. _Requests organization: https://github.com/requests