Merge pull request #1009 from Lukasa/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Auth documentation.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents d0f2382, a7c5d5e · Browse files · 3 files·+85 added−74 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−74 removed
- other:+85 added−74 removed
Provenance
- Commit
4701243- Tree
34f5a76- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
d0f2382- Parent 2
a7c5d5e
3 files changed+85 added−74 removed
added:docs/user/authentication.rst+84 added
1+.. _authentication:2+3+Authentication4+==============5+6+This document discusses using various kinds of authentication with Requests.7+8+Many web services require authentication, and there are many different types.9+Below, we outline various forms of authentication available in Requests, from10+the simple to the complex.11+12+13+Basic Authentication14+--------------------15+16+Many web services that require authentication accept HTTP Basic Auth. This is17+the simplest kind, and Requests supports it straight out of the box.18+19+Making requests with HTTP Basic Auth is very simple::20+21+ >>> from requests.auth import HTTPBasicAuth22+ >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))23+ <Response [200]>24+25+In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand26+for using it::27+28+ >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))29+ <Response [200]>30+31+Providing the credentials in a tuple like this is exactly the same as the32+``HTTPBasicAuth`` example above.33+34+35+Digest Authentication36+---------------------37+38+Another very popular form of HTTP Authentication is Digest Authentication,39+and Requests supports this out of the box as well::40+41+ >>> 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]>45+46+47+Other Authentication48+--------------------49+50+Requests is designed to allow other forms of authentication to be easily and51+quickly plugged in. Members of the open-source community frequently write52+authentication handlers for more complicated or less commonly-used forms of53+authentication. Some of the best have been brought together under the54+`Requests organization`_, including:55+56+- OAuth_57+- Kerberos_58+- NTLM_59+60+If you want to use any of these forms of authentication, go straight to their61+Github page and follow the instructions.62+63+64+New Forms of Authentication65+---------------------------66+67+If you can't find a good implementation of the form of authentication you68+want, you can implement it yourself. Requests makes it easy to add your own69+forms of authentication.70+71+To do so, subclass :class:`requests.auth.AuthBase` and implement the72+``__call__()`` method. When an authentication handler is attached to a request,73+it is called during request setup. The ``__call__`` method must therefore do74+whatever is required to make the authentication work. Some forms of75+authentication will additionally add hooks to provide further functionality.76+77+Examples can be found under the `Requests organization`_ and in the78+``auth.py`` file.79+80+.. _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-74 removed
335 unmodified lines336 >>> r.text337 '{"cookies": {"cookies_are": "working"}}'338 339 340-Basic Authentication341---------------------342-343-Many web services require authentication. There are many different types of344-authentication, but the most common is HTTP Basic Auth.345-346-Making requests with Basic Auth is extremely simple::347-348- >>> from requests.auth import HTTPBasicAuth349- >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))350- <Response [200]>351-352-Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for353-this authentication method::354-355- >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))356- <Response [200]>357-358-Providing the credentials as a tuple in this fashion is functionally equivalent359-to the ``HTTPBasicAuth`` example above.360-361-362-Digest Authentication363----------------------364-365-Another popular form of web service protection is Digest Authentication::366-367- >>> 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]>371-372-373-OAuth Authentication374---------------------375-376-Requests features robust, built-in OAuth support!377-378-OAuth takes many forms, so let's take a look at a few different forms::379-380- import requests381- from requests.auth import OAuth1382-383- url = u'https://api.twitter.com/1/account/settings.json'384-385- client_key = u'...'386- client_secret = u'...'387- resource_owner_key = u'...'388- resource_owner_secret = u'...'389-390-391-Query signing::392-393- queryoauth = OAuth1(client_key, client_secret,394- resource_owner_key, resource_owner_secret,395- signature_type='query')396- r = requests.get(url, auth=queryoauth)397-398-Header signing::399-400- headeroauth = OAuth1(client_key, client_secret,401- resource_owner_key, resource_owner_secret,402- signature_type='auth_header')403- r = requests.get(url, auth=headeroauth)404-405-Body signing::406-407- bodyoauth = OAuth1(client_key, client_secret,408- resource_owner_key, resource_owner_secret,409- signature_type='body')410-411- r = requests.post(url, auth=bodyoauth)412-413-340 Redirection and History341 -----------------------342 343 Requests will automatically perform location redirection while using the GET78 unmodified linesmodified:docs/index.rst+1 added
88 unmodified lines89 user/intro90 user/install91 user/quickstart92 user/advanced93+ user/authentication94 95 96 Community Guide97 -----------------36 unmodified lines