Skip to content
Myr

authentication.rst

This file

Path

docs/user/authentication.rst

146 lines · 5.3 KiB

Last changed by

Merge pull request #4805 from jdufresne/httpsNate Prewittbefore adoptionff0c325

History of this file

Take it

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

The library raises NoneType error when file-pointer (fp) resolves to None.Rajiv Mayanibefore adoption8546a15

lines 1–55why, one step back
1.. _authentication:
23Authentication4==============56.. image:: https://farm5.staticflickr.com/4258/35550409215_3b08d49d22_k_d.jpg78This document discusses using various kinds of authentication with Requests.910Many web services require authentication, and there are many different types.11Below, we outline various forms of authentication available in Requests, from12the simple to the complex.131415Basic Authentication16--------------------1718Many web services that require authentication accept HTTP Basic Auth. This is19the simplest kind, and Requests supports it straight out of the box.2021Making requests with HTTP Basic Auth is very simple::2223 >>> from requests.auth import HTTPBasicAuth24 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))25 <Response [200]>2627In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand28for using it::2930 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))31 <Response [200]>3233Providing the credentials in a tuple like this is exactly the same as the34``HTTPBasicAuth`` example above.353637netrc Authentication38~~~~~~~~~~~~~~~~~~~~3940If no authentication method is given with the ``auth`` argument, Requests will41attempt to get the authentication credentials for the URL's hostname from the42user's netrc file. The netrc file overrides raw HTTP authentication headers43set with `headers=`.4445If credentials for the hostname are found, the request is sent with HTTP Basic46Auth.474849Digest Authentication50---------------------5152Another very popular form of HTTP Authentication is Digest Authentication,53and Requests supports this out of the box as well::5455 >>> from requests.auth import HTTPDigestAuth

Merge pull request #4805 from jdufresne/httpsNate Prewittbefore adoptionff0c325

line 56why, one step back
56 >>> url = 'https://httpbin.org/digest-auth/auth/user/pass'

The library raises NoneType error when file-pointer (fp) resolves to None.Rajiv Mayanibefore adoption8546a15

lines 57–124why, one step back
57 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
58 <Response [200]>596061OAuth 1 Authentication62----------------------6364A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``65library allows Requests users to easily make OAuth 1 authenticated requests::6667 >>> import requests68 >>> from requests_oauthlib import OAuth16970 >>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'71 >>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',72 ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')7374 >>> requests.get(url, auth=auth)75 <Response [200]>7677For more information on how to OAuth flow works, please see the official `OAuth`_ website.78For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_79repository on GitHub8081OAuth 2 and OpenID Connect Authentication82-----------------------------------------8384The ``requests-oauthlib`` library also handles OAuth 2, the authentication mechanism85underpinning OpenID Connect. See the `requests-oauthlib OAuth2 documentation`_ for86details of the various OAuth 2 credential management flows:8788* `Web Application Flow`_89* `Mobile Application Flow`_90* `Legacy Application Flow`_91* `Backend Application Flow`_9293Other Authentication94--------------------9596Requests is designed to allow other forms of authentication to be easily and97quickly plugged in. Members of the open-source community frequently write98authentication handlers for more complicated or less commonly-used forms of99authentication. Some of the best have been brought together under the100`Requests organization`_, including:101102- Kerberos_103- NTLM_104105If you want to use any of these forms of authentication, go straight to their106GitHub page and follow the instructions.107108109New Forms of Authentication110---------------------------111112If you can't find a good implementation of the form of authentication you113want, you can implement it yourself. Requests makes it easy to add your own114forms of authentication.115116To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the117``__call__()`` method::118119 >>> import requests120 >>> class MyAuth(requests.auth.AuthBase):121 ... def __call__(self, r):122 ... # Implement my authentication123 ... return r124 ...

Merge pull request #4805 from jdufresne/httpsNate Prewittbefore adoptionff0c325

line 125why, one step back
125 >>> url = 'https://httpbin.org/get'

The library raises NoneType error when file-pointer (fp) resolves to None.Rajiv Mayanibefore adoption8546a15

lines 126–136why, one step back
126 >>> requests.get(url, auth=MyAuth())
127 <Response [200]>128129When an authentication handler is attached to a request,130it is called during request setup. The ``__call__`` method must therefore do131whatever is required to make the authentication work. Some forms of132authentication will additionally add hooks to provide further functionality.133134Further examples can be found under the `Requests organization`_ and in the135``auth.py`` file.136

Merge pull request #4805 from jdufresne/httpsNate Prewittbefore adoptionff0c325

line 137why, one step back
137.. _OAuth: https://oauth.net/

The library raises NoneType error when file-pointer (fp) resolves to None.Rajiv Mayanibefore adoption8546a15

lines 138–146why, one step back
138.. _requests_oauthlib: https://github.com/requests/requests-oauthlib
139.. _requests-oauthlib OAuth2 documentation: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html140.. _Web Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#web-application-flow141.. _Mobile Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#mobile-application-flow142.. _Legacy Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow143.. _Backend Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow144.. _Kerberos: https://github.com/requests/requests-kerberos145.. _NTLM: https://github.com/requests/requests-ntlm146.. _Requests organization: https://github.com/requests