Google App EngineでTwitter OAuthを使う。
自分のタイムラインを取得してtweetできるまでのサンプル。
参考
tweepyでtwitterの3-legged OAuth認証を試してみた(GoogleAppEngine)
コードのなかで使用しているsimple_cookieは
Google Cookbook – Google App Engine A simple Cookie class のもの。
注意する点は
Twitter application settingでcallback URLを正しく設定すること。

callback URLに127.0.0.1を指定しているからかもしれないけど、なにも入力しないと
TweepError: HTTP Error 401: Unauthorized になる。
tweepy
はapp engineのディレクトリにモジュールのディレクトリをコピーして使った。
以下サンプルコードと実行
~/oauth_twitter/oauth_twitter.py
from google.appengine.ext import webapp |
from google.appengine.ext.webapp.util import run_wsgi_app |
from google.appengine.ext import db |
from google.appengine.api import memcache |
from simple_cookie import Cookies |
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxx' |
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxx' |
log = logging.getLogger(__file__) |
class RequestToken(db.Model): |
token = db.StringProperty() |
secret = db.StringProperty() |
class Home(webapp.RequestHandler): |
cookie = Cookies( self , max_age = SESSION_EXPIRE) |
cookie[ 'sid' ] = str (uuid.uuid4()) |
(timeline, username) = ( None , None ) |
access_token = memcache.get(cookie[ 'sid' ]) |
log.info( 'access_token....%s' % access_token) |
self .response.headers[ 'Content-Type' ] = 'text/html' |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) |
auth.set_access_token(access_token.key, access_token.secret) |
username = auth.get_username() |
log.info( 'username....%s' % username) |
api = tweepy.API(auth_handler = auth) |
timeline = api.home_timeline(count = 10 ) |
self .response.out.write(html) |
self .response.out.write( '<p>' .join([t.text for t in timeline])) |
self .response.out.write( '</body></html>' ) |
access_token = memcache.get(cookie[ 'sid' ]) |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) |
auth.set_access_token(access_token.key, access_token.secret) |
api = tweepy.API(auth_handler = auth) |
api.update_status(status = self .request.get( 'status' )) |
class OAuth(webapp.RequestHandler): |
auth = tweepy.OAuthHandler(CONSUMER_KEY, |
auth_url = auth.get_authorization_url() |
request_token = RequestToken( |
token = auth.request_token.key, |
secret = auth.request_token.secret) |
class OAuthCallBack(webapp.RequestHandler): |
request_token_key = self .request.get( "oauth_token" ) |
request_verifier = self .request.get( 'oauth_verifier' ) |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) |
request_token = RequestToken.gql( "WHERE token=:1" , |
auth.set_request_token(request_token.token, |
access_token = auth.get_access_token(request_verifier) |
log.info(access_token.key) |
log.info(access_token.secret) |
memcache. set (cookie[ 'sid' ], access_token, SESSION_EXPIRE) |
class Logout(webapp.RequestHandler): |
memcache.delete(cookie[ 'sid' ]) |
( '/oauth/callback' , OAuthCallBack), |
application = webapp.WSGIApplication( |
run_wsgi_app(application) |
if __name__ = = '__main__' : |
~/oauth_twitter/app.yaml
application: oauthtwitter # _ はapplicationの識別子として使えないので注意 |
$ dev_appserver.py oauth_twitter |
http://127.0.0.1:8080にアクセスするとtwitterのoauthのページ

許可するとhttp://127.0.0.1:8080/oauth/callbackにリダイレクト

access_tokenを設定して、http://127.0.0.1:8080/にリダイレクト

Posted: May 23rd, 2010 | Author: yamakk | Filed under: 技術 | Tags: gae, google, oauth, tweepy, twitter | No Comments »
Leave a Reply