oscardelben (owner)

Revisions

gist: 204165 Download_button fork
public
Public Clone URL: git://gist.github.com/204165.git
Embed All Files: show embed
Python #
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
import oauth.oauth as oauth
import httplib
 
class ZooppaClient(object):
    # consumer_key, consumer_secret are your api key and secret as strings
    def __init__(self, consumer_key, consumer_secret):
      self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
      self.host = 'localhost'
      self.port = 3000
      if self.port == 80:
          self.authority = self.host
      else:
          self.authority = "%s:%d" % (self.host, self.port)
 
    # path is the path to the resource you wish to access in the REST API, without the leading /api
    # token is an OAuthToken from the oauth library
    def get(self, path, token=None):
      if token:
        token = oauth.OAuthToken.from_string(token)
        
      url = "http://%s%s" % (self.host, path)
      request = oauth.OAuthRequest.from_consumer_and_token(
          self.consumer,
          token,
          http_method='GET',
          http_url=url
      )
      return self._send_request(request, token)
 
    # path is the path to the resource you wish to access in the REST API, without the leading /api
    # post_params is a dictionary of key-value pairs to be serialized into the post body
    # token is an OAuthToken from the oauth library
    def post(self, path, post_params, token=None):
      if token:
        token = oauth.OAuthToken.from_string(token)
        
      url = "http://%s%s" % (self.host, path)
      request = oauth.OAuthRequest.from_consumer_and_token(
          self.consumer,
          token,
          http_method='POST',
          http_url=url,
          parameters=post_params
      )
      return self._send_request(request, token)
 
    # Create a request token and authorization url that will redirect the user to callback_url when they authorize the application
    # returns a [auth_url:string, token:OAuthToken] array where the token is current unauthorized
    # The callback_url will be redirected_to on success with ?oauth_token=XXXXXXXXXXX, so save the token returned here to
    # some persistent store (like a database), and you can mark it as valid when the callback succeeds
    # After you get a validated request token, you can exchange it for an access token (see exchange_request_token_for_access_token)
    def make_request_token_and_authorization_url(self, callback_url):
      response = self.get("/oauth/request_token").read()
      token = oauth.OAuthToken.from_string(response)
      request = oauth.OAuthRequest.from_token_and_callback(token=token, callback=callback_url, http_url='http://%s/oauth/authorize' % self.authority)
      return [token, request.to_url()]
 
    # Exchange a validated request token for an access token
    def exchange_request_token_for_access_token(self, request_token, oauth_verifier):
      path = '/oauth/access_token?oauth_verifier=' + oauth_verifier
      
      response = self.get(path, request_token).read()
      return oauth.OAuthToken.from_string(response)
 
    def _send_request(self, request, token=None):
      request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, token)
      conn = self._get_conn()
      if request.http_method == 'POST':
          conn.request('POST', request.http_url, body=request.to_postdata())
      else:
          conn.request('GET', request.http_url, headers=request.to_header())
      return conn.getresponse()
 
    def _get_conn(self):
      return httplib.HTTPConnection("%s:%d" % (self.host, self.port))