Giter Site home page Giter Site logo

shezi / requests-oauthlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from requests/requests-oauthlib

0.0 2.0 0.0 108 KB

OAuthlib support for Python-Requests!

Home Page: https://crate.io/packages/requests-oauthlib/

License: ISC License

Python 100.00%

requests-oauthlib's Introduction

Requests-OAuthlib

This project provides first-class OAuth library support for Requests.

The OAuth workflow

OAuth can seem overly complicated and it sure has its quirks. Luckily, requests_oauthlib hides most of these and let you focus at the task at hand.

You will be forced to go through a few steps when you are using OAuth. Below is an example of the most common OAuth workflow using HMAC-SHA1 signed requests where the signature is supplied in the Authorization header.

The example assumes an interactive prompt which is good for demonstration but in practice you will likely be using a web application (which makes authorizing much less awkward since you can simply redirect).

  1. Manual client signup with the OAuth provider (i.e. Google, Twitter) to get a set of client credentials. Usually a client key and secret. Client might sometimes be referred to as consumer:

    >>> from __future__ import unicode_literals
    >>> import requests
    >>> from requests_oauthlib import OAuth1
    
    >>> client_key = '...'
    >>> client_secret = '...'
  2. Obtain a request token which will identify you (the client) in the next step. At this stage you will only need your client key and secret.:

    >>> oauth = OAuth1(client_key, client_secret=client_secret)
    >>> request_token_url = https://api.twitter.com/oauth/request_token
    >>> r = requests.post(url=request_token_url, auth=oauth)
    >>> r.content
    "oauth_token=Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik&oauth_token_secret=Kd75W4OQfb2oJTV0vzGzeXftVAwgMnEK9MumzYcM"
    >>> from urlparse import parse_qs
    >>> credentials = parse_qs(r.content)
    >>> resource_owner_key = credentials.get('oauth_token')
    >>> resource_owner_secret = credentials.get('oauth_token_secret')
  3. Obtain authorization from the user (resource owner) to access their protected resources (images, tweets, etc.). This is commonly done by redirecting the user to a specific url to which you add the request token as a query parameter. Note that not all services will give you a verifier even if they should. Also the oauth_token given here will be the same as the one in the previous step:

    >>> authorize_url = 'https://api.twitter.com/oauth/authorize?oauth_token='
    >>> authorize_url = authorize_url + resource_owner_key
    >>> print 'Please go here and authorize,', authorize_url
    >>> verifier = raw_input('Please input the verifier')
  4. Obtain an access token from the OAuth provider. Save this token as it can be re-used later. In this step we will re-use most of the credentials obtained uptil this point:

    >>> oauth = OAuth1(client_key,
                       client_secret=client_secret,
                       resource_owner_key=resource_owner_key,
                       resource_owner_secret=resource_owner_secret,
                       verifier=verifier)
    >>> access_token_url = https://api.twitter.com/oauth/access_token
    >>> r = requests.post(url=access_token_url, auth=oauth)
    >>> r.content
    "oauth_token=6253282-eWudHldSbIaelX7swmsiHImEL4KinwaGloHANdrY&oauth_token_secret=2EEfA6BG3ly3sR3RjE0IBSnlQu4ZrUzPiYKmrkVU"
    >>> credentials = parse_qs(r.content)
    >>> resource_owner_key = credentials.get('oauth_token')
    >>> resource_owner_secret = credentials.get('oauth_token_secret')
  5. Access protected resources. OAuth1 access tokens typically do not expire and may be re-used until revoked by the user or you:

    >>> oauth = OAuth1(client_key,
                       client_secret=client_secret,
                       resource_owner_key=resource_owner_key,
                       resource_owner_secret=resource_owner_secret)
    >>> url = 'https://api.twitter.com/1/account/settings.json'
    >>> r = requests.get(url=url, auth=oauth)
    >>> # Enjoy =)

Signature placement - header, query or body?

OAuth takes many forms, so let's take a look at a few different forms:

import requests
from requests_oauthlib import OAuth1

url = u'https://api.twitter.com/1/account/settings.json'

client_key = u'...'
client_secret = u'...'
resource_owner_key = u'...'
resource_owner_secret = u'...'

Header signing (recommended):

headeroauth = OAuth1(client_key, client_secret,
                     resource_owner_key, resource_owner_secret,
                     signature_type='auth_header')
r = requests.get(url, auth=headeroauth)

Query signing:

queryoauth = OAuth1(client_key, client_secret,
                    resource_owner_key, resource_owner_secret,
                    signature_type='query')
r = requests.get(url, auth=queryoauth)

Body signing:

bodyoauth = OAuth1(client_key, client_secret,
                   resource_owner_key, resource_owner_secret,
                   signature_type='body')

r = requests.post(url, auth=bodyoauth)

Signature types - HMAC (most common), RSA, Plaintext

OAuth1 defaults to using HMAC and examples can be found in the previous sections.

Plaintext work on the same credentials as HMAC and the only change you will need to make when using it is to add signature_type='PLAINTEXT' to the OAuth1 constructor:

headeroauth = OAuth1(client_key, client_secret,
                     resource_owner_key, resource_owner_secret,
                     signature_method='PLAINTEXT')

RSA is different in that it does not use client_secret nor resource_owner_secret. Instead it uses public and private keys. The public key is provided to the OAuth provider during client registration. The private key is used to sign requests. The previous section can be summarized as:

key = open("your_rsa_key.pem").read()

queryoauth = OAuth1(client_key, signature_method=SIGNATURE_RSA,
                    rsa_key=key, signature_type='query')
headeroauth = OAuth1(client_key, signature_method=SIGNATURE_RSA,
                    rsa_key=key, signature_type='auth_header')
bodyoauth = OAuth1(client_key, signature_method=SIGNATURE_RSA,
                    rsa_key=key, signature_type='body')

Installation

To install requests and requests_oauthlib you can use pip:

$ pip install requests requests_oauthlib

requests-oauthlib's People

Contributors

ib-lundgren avatar kracekumar avatar lukasa avatar mart-e avatar matthewlmcclure avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.