Random ...
December 2017
          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
Tags ...

python+zope++:: python, zope2 and twitter/oauth: oh my!
Posted at 13.Apr,2009 11:56  Comments 1 / Trackbacks 0 / Like this post!
Technorati tag(s):

Getting python, zope2 and oauth to dance takes efforts. Understanding how OAuth works is essential.

However, the oauth draft is too long for eager developers to grok. But fret not, read the quick and dirty guide to oauth and we'd be on our way.

Here's what I did to get python, zope and twitter work together via oauth.

First, create your app key

Get oauth python client

Since this is a quick exercise, We will use External Method. Here's our file that should be put in Extensions directory:

 # -*- coding: utf-8 -*-

 import httplib, oauth
 import simplejson as sj

 CONSUMER_KEY='your consumerkeyfrom your app'
 CONSUMER_SECRET='your consumer secret frim your app'
 consumer=oauth.OAuthConsumer(CONSUMER_KEY,  CONSUMER_SECRET)
 signature_method_hmac_sha1 =  oauth.OAuthSignatureMethod_HMAC_SHA1()
 TWITTER_CHECK_AUTH = 'https://twitter.com/account/verify_credentials.json'
 TWITTER_FRIENDS = 'http://twitter.com/statuses/friends.json'
 TWITTER_STATUS = 'http://twitter.com/statuses/friends_timeline.json'

 def obtainRequestToken(self):
    oauth_req = oauth.OAuthRequest.from_consumer_and_token(consumer, http_url=REQUEST_TOKEN_URL)
    oauth_req.sign_request(signature_method_hmac_sha1, consumer, None)
    print "request via headers obtainRequestToken", oauth_req.parameters
    print "token keys", token.key, token
    print "parameters", oauth_req.parameters

    return token

 def authorizeRequestToken(self, token):
    print 'access token',token, type(token)
    oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=AUTHORIZATION_URL)
    print 'REQUEST (via url query string)authorizeRequestToke'
    print 'parameters: %s' % str(oauth_request.parameters)
    print 'token auth', token,'\n----'
    url = oauth_request.to_url()
    return self.REQUEST.RESPONSE.redirect(url)

 def accessToken(self, token):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=ACCESS_TOKEN_URL)
    oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
    print 'REQUEST (via headers) - access url\n'
    print 'parameters: %s' % str(oauth_request.parameters)

    conn.request('GET', ACCESS_TOKEN_URL, headers=oauth_request.to_header()) 
    response = conn.getresponse()
    token = oauth.OAuthToken.from_string(response.read())
    print 'my real token',token,type(token),'\n----'
    print 'access token', token.key, token.secret,'\n----'
    self.REQUEST.SESSION['token'] = token #.key
    return self.REQUEST.RESPONSE.redirect('twit')

 def getStatus(self, token):
    print 'the token from session in getstatus',token,'\n----'
    print token,type(token),'\n----'
    #token = oauth.OAuthToken.from_string(token)

    oauth_request = oauth.OAuthRequest.from_consumer_and_token(
        consumer, token=token, http_url=TWITTER_STATUS, parameters={'page': 1})
    oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)

    url = oauth_request.to_url()
    response = conn.getresponse()
    res = sj.load(response)
    return res

Ignore all the print statements. Following the quick and dirty guide, listed here are the necessary DTML Methods (yeah, yeah) and Script (Python) required to make things work:

 #dtml method id: twit
 #we check for session token and return the login page if not available
 <dtml-var standard_html_header>
 <h2><dtml-var title_or_id> <dtml-var document_title></h2>
 <dtml-if expr="SESSION.has_key('token')">
 <dtml-let token="SESSION['token']">

 <dtml-in expr="getStatus(token)" prefix=seq>
 <dtml-var expr="seq_item['user']['screen_name']">: <dtml-var expr="seq_item['text']"> at <dtml-var expr="seq_item['created_at']"><br>
 <a href="getpage">login</a>

 <dtml-var standard_html_footer>

 #script python id: getpage
 token = context.gettoken() #our first step.  gettoken is an 
                                                 #external method pointing to obtainRequestToken
 session = context.REQUEST.SESSION
 session['token'] = token
 print context.getaccesspage(token)#2nd step, points to
                                               #external method authorizeRequestToken, 
                                               #send to authorize page

  #once users click allow, users will be redirected to Application Website as 
  #specified in our twitter app page, e.g http://host.domain/twitest
 #script python id: twitest
 token = context.REQUEST.SESSION['token']
 context.accessUrl(token) #accessUrl is external method pointing to 
                                          #accessToken which will redirect to twit, our main page

 #and to finish things off, we get status from twitter and iterate thru.  see above

It's easy once we got the hang of it. Thanks to the quick and dirty guide, python oauth client (see the examples at their page), python oauth samples (is this tav who used to hang at #zope in the 90's?) and django example

Now that we got the basics done, we can turn this to a Zope 2 product! Are there any new zope2 users nowadays ? But that's for another day :P

Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/234/tbping 



kedai  at 14.Apr,2009 13:14
Post a comment