authentication.rst
This file
Path
docs/user/authentication.rst
145 lines · 5.3 KiB
Last changed by
Merge pull request #6018 from nateprewitt/basic_auth_updateIan Stapleton Cordascobefore adoptione2064a2
History of this fileTake it
The walk stopped after 200 commits: lines older than 8c211a9 point at it.
Merge pull request #5681 from mateusduboli/5677Ian Stapleton Cordascobefore adoption8c211a9
lines 1–21why, 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 HTTPBasicAuthMerge pull request #6018 from nateprewitt/basic_auth_updateIan Stapleton Cordascobefore adoptione2064a2
lines 22–23why, one step back22 >>> basic = HTTPBasicAuth('user', 'pass')23 >>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)Merge pull request #5681 from mateusduboli/5677Ian Stapleton Cordascobefore adoption8c211a9
lines 24–28why, one step back24 <Response [200]>2526In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand27for using it::28Merge pull request #6018 from nateprewitt/basic_auth_updateIan Stapleton Cordascobefore adoptione2064a2
line 29why, one step back29 >>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))Merge pull request #5681 from mateusduboli/5677Ian Stapleton Cordascobefore adoption8c211a9
lines 30–145why, one step back30 <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.464748Digest Authentication49---------------------5051Another very popular form of HTTP Authentication is Digest Authentication,52and Requests supports this out of the box as well::5354 >>> from requests.auth import HTTPDigestAuth55 >>> url = 'https://httpbin.org/digest-auth/auth/user/pass'56 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))57 <Response [200]>585960OAuth 1 Authentication61----------------------6263A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``64library allows Requests users to easily make OAuth 1 authenticated requests::6566 >>> import requests67 >>> from requests_oauthlib import OAuth16869 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'70 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',71 ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')7273 >>> requests.get(url, auth=auth)74 <Response [200]>7576For more information on how to OAuth flow works, please see the official `OAuth`_ website.77For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_78repository on GitHub7980OAuth 2 and OpenID Connect Authentication81-----------------------------------------8283The ``requests-oauthlib`` library also handles OAuth 2, the authentication mechanism84underpinning OpenID Connect. See the `requests-oauthlib OAuth2 documentation`_ for85details of the various OAuth 2 credential management flows:8687* `Web Application Flow`_88* `Mobile Application Flow`_89* `Legacy Application Flow`_90* `Backend Application Flow`_9192Other Authentication93--------------------9495Requests is designed to allow other forms of authentication to be easily and96quickly plugged in. Members of the open-source community frequently write97authentication handlers for more complicated or less commonly-used forms of98authentication. Some of the best have been brought together under the99`Requests organization`_, including:100101- Kerberos_102- NTLM_103104If you want to use any of these forms of authentication, go straight to their105GitHub page and follow the instructions.106107108New Forms of Authentication109---------------------------110111If you can't find a good implementation of the form of authentication you112want, you can implement it yourself. Requests makes it easy to add your own113forms of authentication.114115To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the116``__call__()`` method::117118 >>> import requests119 >>> class MyAuth(requests.auth.AuthBase):120 ... def __call__(self, r):121 ... # Implement my authentication122 ... return r123 ...124 >>> url = 'https://httpbin.org/get'125 >>> requests.get(url, auth=MyAuth())126 <Response [200]>127128When an authentication handler is attached to a request,129it is called during request setup. The ``__call__`` method must therefore do130whatever is required to make the authentication work. Some forms of131authentication will additionally add hooks to provide further functionality.132133Further examples can be found under the `Requests organization`_ and in the134``auth.py`` file.135136.. _OAuth: https://oauth.net/137.. _requests_oauthlib: https://github.com/requests/requests-oauthlib138.. _requests-oauthlib OAuth2 documentation: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html139.. _Web Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#web-application-flow140.. _Mobile Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#mobile-application-flow141.. _Legacy Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow142.. _Backend Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow143.. _Kerberos: https://github.com/requests/requests-kerberos144.. _NTLM: https://github.com/requests/requests-ntlm145.. _Requests organization: https://github.com/requests