Skip to content
Myr

authentication.rst

This file

Path

docs/user/authentication.rst

128 lines · 4.1 KiB

Last changed by

Merge pull request #2138 from dpursehouse/more-doc-updatesCory Benfieldbefore adoption587a515

History of this file

Take it

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

Merge pull request #1507 from buzztabapp/prepared-session-requestsKenneth Reitzbefore adoption3e6e68b

lines 1–101why, 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.4142If credentials for the hostname are found, the request is sent with HTTP Basic43Auth.444546Digest 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]>565758OAuth 1 Authentication59----------------------6061A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``62library allows Requests users to easily make OAuth authenticated requests::6364 >>> import requests65 >>> 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.75For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_76repository on GitHub777879Other 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 their92GitHub page and follow the instructions.939495New 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.101

Merge pull request #2138 from dpursehouse/more-doc-updatesCory Benfieldbefore adoption587a515

line 102why, one step back
102To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the

Merge pull request #1507 from buzztabapp/prepared-session-requestsKenneth Reitzbefore adoption3e6e68b

lines 103–128why, 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,116it 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.119120Further examples can be found under the `Requests organization`_ and in the121``auth.py`` file.122123.. _OAuth: http://oauth.net/124.. _requests_oauthlib: https://github.com/requests/requests-oauthlib125.. _Kerberos: https://github.com/requests/requests-kerberos126.. _NTLM: https://github.com/requests/requests-ntlm127.. _Requests organization: https://github.com/requests128