Skip to content
Myr

authentication.rst

This file

Path

docs/user/authentication.rst

144 lines · 5.2 KiB

Last changed by

Merge pull request #3804 from ncoghlan/patch-1Cory Benfieldbefore adoption1c69fc2

History of this file

Take it

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

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

lines 1–62why, 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.333435netrc 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. The netrc file overrides raw HTTP authentication headers41set with `headers=`.4243If credentials for the hostname are found, the request is sent with HTTP Basic44Auth.454647Digest Authentication48---------------------4950Another very popular form of HTTP Authentication is Digest Authentication,51and Requests supports this out of the box as well::5253 >>> from requests.auth import HTTPDigestAuth54 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'55 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))56 <Response [200]>575859OAuth 1 Authentication60----------------------6162A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``

Merge pull request #3804 from ncoghlan/patch-1Cory Benfieldbefore adoption1c69fc2

line 63why, one step back
63library allows Requests users to easily make OAuth 1 authenticated requests::

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

lines 64–69why, one step back
64
65 >>> import requests66 >>> from requests_oauthlib import OAuth16768 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'69 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',

Merge pull request #3658 from elzapp/patch-1Cory Benfieldbefore adoption7eaa5ee

line 70why, one step back
70 ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

lines 71–78why, one step back
71
72 >>> requests.get(url, auth=auth)73 <Response [200]>7475For more information on how to OAuth flow works, please see the official `OAuth`_ website.76For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_77repository on GitHub78

Merge pull request #3804 from ncoghlan/patch-1Cory Benfieldbefore adoption1c69fc2

lines 79–80why, one step back
79OAuth 2 and OpenID Connect Authentication
80-----------------------------------------

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

line 81why, one step back
81

Merge pull request #3804 from ncoghlan/patch-1Cory Benfieldbefore adoption1c69fc2

lines 82–90why, one step back
82The ``requests-oauthlib`` library also handles OAuth 2, the authentication mechanism
83underpinning OpenID Connect. See the `requests-oauthlib OAuth2 documentation`_ for84details of the various OAuth 2 credential management flows:8586* `Web Application Flow`_87* `Mobile Application Flow`_88* `Legacy Application Flow`_89* `Backend Application Flow`_90

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

lines 91–136why, one step back
91Other Authentication
92--------------------9394Requests is designed to allow other forms of authentication to be easily and95quickly plugged in. Members of the open-source community frequently write96authentication handlers for more complicated or less commonly-used forms of97authentication. Some of the best have been brought together under the98`Requests organization`_, including:99100- Kerberos_101- NTLM_102103If you want to use any of these forms of authentication, go straight to their104GitHub page and follow the instructions.105106107New Forms of Authentication108---------------------------109110If you can't find a good implementation of the form of authentication you111want, you can implement it yourself. Requests makes it easy to add your own112forms of authentication.113114To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the115``__call__()`` method::116117 >>> import requests118 >>> class MyAuth(requests.auth.AuthBase):119 ... def __call__(self, r):120 ... # Implement my authentication121 ... return r122 ...123 >>> url = 'http://httpbin.org/get'124 >>> requests.get(url, auth=MyAuth())125 <Response [200]>126127When an authentication handler is attached to a request,128it is called during request setup. The ``__call__`` method must therefore do129whatever is required to make the authentication work. Some forms of130authentication will additionally add hooks to provide further functionality.131132Further examples can be found under the `Requests organization`_ and in the133``auth.py`` file.134135.. _OAuth: http://oauth.net/136.. _requests_oauthlib: https://github.com/requests/requests-oauthlib

Merge pull request #3804 from ncoghlan/patch-1Cory Benfieldbefore adoption1c69fc2

lines 137–141why, one step back
137.. _requests-oauthlib OAuth2 documentation: http://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html
138.. _Web Application Flow: http://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#web-application-flow139.. _Mobile Application Flow: http://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#mobile-application-flow140.. _Legacy Application Flow: http://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow141.. _Backend Application Flow: http://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow

Merge pull request #3557 from sigmavirus24/fix-failing-testCory Benfieldbefore adoption2041adb

lines 142–144why, one step back
142.. _Kerberos: https://github.com/requests/requests-kerberos
143.. _NTLM: https://github.com/requests/requests-ntlm144.. _Requests organization: https://github.com/requests