Quick single-user twitter oauth setup
I had a tool that posted to twitter with a username and password that were saved in a neighboring file. Here is how I ported that setup to oauth. This procedure is not at all friendly to multiple users who would run your tool; it's only suitable if making a user account is something you do roughly once (like you used to do by pasting in the user+password into your config file).
Go to the new app page and make up some details. Application type is 'client'. No URLs matter.
Run this to get two new values:
% ipython
import oauth2, urlparse
consumer = oauth2.Consumer("consumer key goes here", "consumer secret goes here")
client = oauth2.Client(consumer)
resp, content = client.request("https://api.twitter.com/oauth/request_token")
request_token = dict(urlparse.parse_qsl(content))
print "%s?oauth_token=%s" % ("https://api.twitter.com/oauth/authorize", request_token['oauth_token'])
Visit that link, get a PIN.
token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier("pin goes here")
client = oauth2.Client(consumer, token)
resp, content = client.request("https://api.twitter.com/oauth/access_token", "POST")
access_token = dict(urlparse.parse_qsl(content))
{"oauth_token" : access_token['oauth_token'], "oauth_token_secret" : access_token['oauth_token_secret']}
Save those two values with the consumer key and consumer secret you got from twitter during registration.
All that was the one-off process for getting all the magic cookies. If you now want to talk to twitter from python, import this:
def makeOauthFilter(conf):
consumer = restkit.util.oauth2.Consumer(conf['oauth_consumer_key'],
conf['oauth_consumer_secret'])
token = restkit.util.oauth2.Token(conf['oauth_token'],
conf['oauth_token_secret'])
return restkit.filters.oauth2.OAuthFilter("*", consumer, token,
restkit.util.oauth2.SignatureMethod_HMAC_SHA1())
oauthFilter = makeOauthFilter(conf)
timelineJs = restkit.request(
method="GET",
url="http://api.twitter.com/1/statuses/home_timeline.json",
filters=[oauthFilter],
since_id=since_id).body_string()
for status in jsonlib.read(timelineJs, use_float=True):
...
- New comment
Atom feed of this blog