PythonでOAuthを試したかったのでtweepyを使ってみた。
easy_installじゃなくてgitから
git clone git://github.com/joshthecoder/tweepy.git
まずはBasic認証
>>> import tweepy
>>> auth = tweepy.BasicAuthHandler("user", "password")
>>> api = tweepy.API(auth)
>>> api.update_status('hello from tweepy(Basic)')
<tweepy.models.Status object at 0x13c8db0>
http://twitter.com/kzfm/status/9330123865
続いてOAuth。consumer_token,consumer_secretはあらかじめtwitterから取得しておく。
>>> import tweepy
>>> consumer_token = "xxxxxxxxxxxxxx"
>>> consumer_secret = "xxxxxxxxxxxxxxxx"
>>> auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
>>> redirect_url = auth.get_authorization_url()
>>> redirect_url
redirect_urlのURLにブラウザでアクセスして許可すると7桁の数字が表示されるのでそれを入力
>>> verifier = '1234567'
>>> auth.get_access_token(verifier)
<tweepy.oauth.OAuthToken object at 0x1393510>
>>> key = auth.access_token.key
>>> secret = auth.access_token.secret
>>> auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
>>> auth.set_access_token(key, secret)
>>> oauthapi = tweepy.API(auth)
>>> oauthapi.update_status('hello from tweepy(OAuth)')
<tweepy.models.Status object at 0x1397b50>
http://twitter.com/kzfm/status/9330367673
wsgioauthもいいかもとか思い始めたでござる。