Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ac5e680 point at it.

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 1–34why, one step back
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.3334

Merge pull request #1465 from dpursehouse/doc-netrc-authenticationCory Benfieldbefore adoptionbe88434

lines 35–45why, one step back
35netrc Authentication
36~~~~~~~~~~~~~~~~~~~~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.4445

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 46–57why, one step back
46Digest Authentication
47---------------------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]>5657

Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1

lines 58–60why, one step back
58OAuth 1 Authentication
59----------------------60

Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f

lines 61–62why, one step back
61A 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 back
63

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7430a49

line 64why, one step back
64 >>> import requests

Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1

lines 65–74why, one step back
65 >>> from requests_oauthlib import OAuth1
6667 >>> 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 back
75For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_
76repository on GitHub

Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1

lines 77–78why, one step back
77
78

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 79–91why, one step back
79Other Authentication
80--------------------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 their

Merge pull request #1474 from Kwpolska/docfixKenneth Reitzbefore adoptiond83919a

line 92why, one step back
92GitHub page and follow the instructions.

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 93–102why, one step back
93
9495New 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 the

Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f

lines 103–115why, one step back
103``__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,

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 116–119why, one step back
116it is called during request setup. The ``__call__`` method must therefore do
117whatever is required to make the authentication work. Some forms of118authentication will additionally add hooks to provide further functionality.119

Merge pull request #1477 from dpursehouse/other-auth-basic-exampleCory Benfieldbefore adoptionec6584f

line 120why, one step back
120Further examples can be found under the `Requests organization`_ and in the

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 121–122why, one step back
121``auth.py`` file.
122

Merge pull request #1305 from michaelhelmick/patch-1Kenneth Reitzbefore adoptionc0baec1

lines 123–124why, one step back
123.. _OAuth: http://oauth.net/
124.. _requests_oauthlib: https://github.com/requests/requests-oauthlib

Merge pull request #1145 from sigmavirus24/masterKenneth Reitzbefore adoptionac5e680

lines 125–128why, one step back
125.. _Kerberos: https://github.com/requests/requests-kerberos
126.. _NTLM: https://github.com/requests/requests-ntlm127.. _Requests organization: https://github.com/requests128