Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (71 sloc) 3.189 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
import oauth2 as oauth
import urllib
import urlparse

if not hasattr(urlparse,'parse_qsl'):
    import cgi
    urlparse.parse_qsl = cgi.parse_qsl

REQUEST_TOKEN, AUTHORIZE, AUTHENTICATE, ACCESS_TOKEN = range(4)
OAUTH_ACTIONS = {
    REQUEST_TOKEN: 'request_token',
    AUTHORIZE: 'authorize',
    AUTHENTICATE: 'authenticate',
    ACCESS_TOKEN: 'access_token',
}

class Client:

    def __init__(self, key, secret, base_url, actions=OAUTH_ACTIONS, force_auth_header=False,
        signature_method=None):

        self.key = key
        self.secret = secret
        self.base_url = base_url
        self.actions = actions
        unimplemented = set(OAUTH_ACTIONS.keys()) - set(actions.keys())
        if unimplemented:
            raise Exception("Missing OAuth actions: %s" % ', '.join(OAUTH_ACTIONS[u] for u in unimplemented))
        self.force_auth_header = force_auth_header
        self.signature_method = signature_method

    def get_url(self, action, token=None, callback_url=None):
        try:
            url = urlparse.urljoin(self.base_url, self.actions[action])
        except KeyError:
            raise Exception("Invalid OAuth action.")

        if token:
            if self.signature_method:
                consumer = oauth.Consumer(self.key, self.secret)
                oauth_request = oauth.Request.from_token_and_callback(
                    token=token, callback=callback_url, http_url=url
                )
                oauth_request.sign_request(self.signature_method, consumer, consumer)
                url = oauth_request.to_url()
            else:
                url = '%s?%s' % (url, urllib.urlencode({
                    'oauth_token': token.key,
                }))
        return url

    def request_token(self):
        consumer = oauth.Consumer(self.key, self.secret)
        client = oauth.Client(consumer)
        request_token_url = self.get_url(REQUEST_TOKEN)
        resp, content = client.request(request_token_url)
        if resp['status'] != '200':
            raise Exception("Invalid response %s." % resp['status'])
        return oauth.Token.from_string(content)

    def access_token(self, request_token, verifier=None):
        consumer = oauth.Consumer(self.key, self.secret)
        client = oauth.Client(consumer, request_token)

        access_token_url = self.get_url(ACCESS_TOKEN)

        if verifier:
            body = "oauth_verifier=%s" % verifier
        else:
            body = None
        resp, content = client.request(access_token_url, method="POST", body=body)
        if resp['status'] != '200':
            raise Exception("Invalid response %s." % resp['status'])
        return dict(urlparse.parse_qsl(content))

    def request(self, url, access_token, method="GET", body=''):
        consumer = oauth.Consumer(self.key, self.secret)
        token = oauth.Token(key=access_token['oauth_token'], secret=access_token['oauth_token_secret'])
        client = oauth.Client(consumer, token=token)
        resp, content = client.request(url, method=method, body=body)
        if resp['status'] != '200':
            raise Exception("Invalid response %s.\r\n%s" % (resp['status'], content))
        return content
Something went wrong with that request. Please try again.