authentication.rst
This file
Path
docs/user/authentication.rst
128 lines · 4.1 KiB
Last changed by
Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f
History of this fileTake it
The walk stopped after 200 commits: lines older than 6e780fa point at it.
remove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 1–34why, 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.3334Merge pull request #1465 from dpursehouse/doc-netrc-authenticationCory Benfieldbefore adoptionbe88434
lines 35–45why, one step back35netrc Authentication36~~~~~~~~~~~~~~~~~~~~3738If no authentication method is given with the ``auth`` argument, Requests will39attempt to get the authentication credentials for the URL's hostname from the40user's netrc file.4142If credentials for the hostname are found, the request is sent with HTTP Basic43Auth.4445remove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 46–57why, one step back46Digest Authentication47---------------------4849Another very popular form of HTTP Authentication is Digest Authentication,50and Requests supports this out of the box as well::5152 >>> from requests.auth import HTTPDigestAuth53 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'54 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))55 <Response [200]>5657Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1
lines 58–60why, one step back58OAuth 1 Authentication59----------------------60Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f
lines 61–62why, one step back61A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``62library allows Requests users to easily make OAuth authenticated requests::Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1
line 63why, one step back63Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7430a49
line 64why, one step back64 >>> import requestsMerge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1
lines 65–74why, one step back65 >>> from requests_oauthlib import OAuth16667 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'68 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',69 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')7071 >>> requests.get(url, auth=auth)72 <Response [200]>7374For more information on how to OAuth flow works, please see the official `OAuth`_ website.Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f
lines 75–76why, one step back75For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_76repository on GitHubMerge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1
lines 77–78why, one step back7778remove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 79–91why, one step back79Other Authentication80--------------------8182Requests is designed to allow other forms of authentication to be easily and83quickly plugged in. Members of the open-source community frequently write84authentication handlers for more complicated or less commonly-used forms of85authentication. Some of the best have been brought together under the86`Requests organization`_, including:8788- Kerberos_89- NTLM_9091If you want to use any of these forms of authentication, go straight to theirMerge pull request #1474 from Kwpolska/docfixKenneth Reitzbefore adoptiond83919a
line 92why, one step back92GitHub page and follow the instructions.remove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 93–102why, one step back939495New Forms of Authentication96---------------------------9798If you can't find a good implementation of the form of authentication you99want, you can implement it yourself. Requests makes it easy to add your own100forms of authentication.101102To do so, subclass :class:`requests.auth.AuthBase` and implement theMerge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f
lines 103–115why, one step back103``__call__()`` method::104105 >>> import requests106 >>> class MyAuth(requests.auth.AuthBase):107 ... def __call__(self, r):108 ... # Implement my authentication109 ... return r110 ...111 >>> url = 'http://httpbin.org/get'112 >>> requests.get(url, auth=MyAuth())113 <Response [200]>114115When an authentication handler is attached to a request,remove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 116–119why, one step back116it is called during request setup. The ``__call__`` method must therefore do117whatever is required to make the authentication work. Some forms of118authentication will additionally add hooks to provide further functionality.119Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f
line 120why, one step back120Further examples can be found under the `Requests organization`_ and in theremove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 121–122why, one step back121``auth.py`` file.122Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1
lines 123–124why, one step back123.. _OAuth: http://oauth.net/124.. _requests_oauthlib: https://github.com/requests/requests-oauthlibremove unused timeoutKenneth Reitzbefore adoption6e780fa
lines 125–128why, one step back125.. _Kerberos: https://github.com/requests/requests-kerberos126.. _NTLM: https://github.com/requests/requests-ntlm127.. _Requests organization: https://github.com/requests128