Tonight's Random Bit Of Code

I nearly went and re-did a couple of my scripts in (using this gem), but fixing the stuff I had was so much more fun:

if not os.path.exists(TOKEN_FILE):
  api = oauthtwitter.OAuthApi(CONSUMER_KEY, CONSUMER_SECRET)
  request_token = api.getRequestToken()
  print api.getAuthorizationURL(request_token)
  api = oauthtwitter.OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)
  access_token = api.getAccessToken(raw_input("Please enter the PIN: "))
  open(TOKEN_FILE,'w').write(pickle.dumps(access_token))
else:
  access_token=pickle.loads(open(TOKEN_FILE,'r').read())
api = oauthtwitter.OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
user = api.getUserInfo()
print user

And yes, it’s a hack in more ways than one, but the python-twitter crowd doesn’t seem to be getting their act together, so I got oauth-python-twitter and beat it up until it worked.

It feels great to be able to tackle tiny problems and solve them for a change.

Update: My tweaked source for oauthtwitter.py and the trunk oauth.py I’m using. To get the consumer keys, register a new app by visiting http://twitter.com/oauth_clients – the rest should be self-explanatory.