authentication.rst
This file
Path
docs/user/authentication.rst
84 lines · 2.8 KiB
Last changed by
Merge pull request #1009 from Lukasa/masterKenneth Reitzbefore adoption4701243
History of this fileTake it
Merge pull request #1009 from Lukasa/masterKenneth Reitzbefore adoption4701243
lines 1–84why, 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.333435Digest Authentication36---------------------3738Another very popular form of HTTP Authentication is Digest Authentication,39and Requests supports this out of the box as well::4041 >>> from requests.auth import HTTPDigestAuth42 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'43 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))44 <Response [200]>454647Other Authentication48--------------------4950Requests is designed to allow other forms of authentication to be easily and51quickly plugged in. Members of the open-source community frequently write52authentication handlers for more complicated or less commonly-used forms of53authentication. Some of the best have been brought together under the54`Requests organization`_, including:5556- OAuth_57- Kerberos_58- NTLM_5960If you want to use any of these forms of authentication, go straight to their61Github page and follow the instructions.626364New Forms of Authentication65---------------------------6667If you can't find a good implementation of the form of authentication you68want, you can implement it yourself. Requests makes it easy to add your own69forms of authentication.7071To do so, subclass :class:`requests.auth.AuthBase` and implement the72``__call__()`` method. When an authentication handler is attached to a request,73it is called during request setup. The ``__call__`` method must therefore do74whatever is required to make the authentication work. Some forms of75authentication will additionally add hooks to provide further functionality.7677Examples can be found under the `Requests organization`_ and in the78``auth.py`` file.7980.. _OAuth: https://github.com/requests/requests-oauthlib81.. _Kerberos: https://github.com/requests/requests-kerberos82.. _NTLM: https://github.com/requests/requests-ntlm83.. _Requests organization: https://github.com/requests84