Skip to content
Myr

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

3 files+85 added−74 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-74 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]>371372373OAuth Authentication374--------------------375376Requests features robust, built-in OAuth support!377378OAuth takes many forms, so let's take a look at a few different forms::379380 import requests381 from requests.auth import OAuth1382383 url = u'https://api.twitter.com/1/account/settings.json'384385 client_key = u'...'386 client_secret = u'...'387 resource_owner_key = u'...'388 resource_owner_secret = u'...'389390391Query signing::392393 queryoauth = OAuth1(client_key, client_secret,394 resource_owner_key, resource_owner_secret,395 signature_type='query')396 r = requests.get(url, auth=queryoauth)397398Header signing::399400 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)404405Body signing::406407 bodyoauth = OAuth1(client_key, client_secret,408 resource_owner_key, resource_owner_secret,409 signature_type='body')410411 r = requests.post(url, auth=bodyoauth)412413340Redirection 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