Skip to content
Myr

Split authentication out into a separate file.

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

authored by Cory Benfield · committed by Cory Benfield · parent 54b77af · Browse files · 3 files+85 added−56 removed
This commit

State

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

Diff stats

3 files+85 added−56 removed

  • other:+85 added−56 removed

3 files+85 added−56 removed

added:docs/user/authentication.rst+84 added
1.. _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
modified:docs/user/quickstart.rst-56 removed
335 unmodified lines
336 >>> r.text337 '{"cookies": {"cookies_are": "working"}}'338339340Basic Authentication341--------------------342343Many web services require authentication. There are many different types of344authentication, but the most common is HTTP Basic Auth.345346Making requests with Basic Auth is extremely simple::347348 >>> from requests.auth import HTTPBasicAuth349 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))350 <Response [200]>351352Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for353this authentication method::354355 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))356 <Response [200]>357358Providing the credentials as a tuple in this fashion is functionally equivalent359to the ``HTTPBasicAuth`` example above.360361362Digest Authentication363---------------------364365Another popular form of web service protection is Digest Authentication::366367 >>> from requests.auth import HTTPDigestAuth368 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'369 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))370 <Response [200]>371372373Other Authentication374--------------------375376Requests is designed to allow other forms of authentication to be easily and377quickly plugged in. Members of the open-source community frequently write378authentication handlers for more complicated or less commonly-used forms of379authentication. Some of the best have been brought together under a single380`organization on Github`_, including:381382- OAuth_383- Kerberos_384- NTLM_385386If you want to use any of these forms of authentication, go straight to their387Github page and follow the instructions. If you can't find the one you want,388why not write one and submit it?389390.. _OAuth: https://github.com/requests/requests-oauthlib391.. _Kerberos: https://github.com/requests/requests-kerberos392.. _NTLM: https://github.com/requests/requests-ntlm393.. _organization on Github: https://github.com/requests394395340Redirection and History341-----------------------342343Requests will automatically perform location redirection while using the GET
78 unmodified lines
modified:docs/index.rst+1 added
88 unmodified lines
89 user/intro90 user/install91 user/quickstart92 user/advanced93 user/authentication949596Community Guide97-----------------
36 unmodified lines