Created
February 8, 2011 18:39
-
-
Save joshfinnie/816940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Python boilerplate for using Twitter API w/OAuth, based on sixohsix-twitter. | |
As of 2010-08-31, Twitter has shut down Basic Auth completely. | |
You must use OAuth instead. This boilerplate does just that. | |
http://dev.twitter.com/pages/basic_auth_shutdown | |
This Python script uses the Python twitter package by Mike Verdone et al. | |
http://pypi.python.org/pypi/twitter | |
http://mike.verdone.ca/twitter/ | |
"Python Twitter Tools (PTT)" | |
Extracted from twitter.cmdline module. For use on Unix-like systems. | |
""" | |
import os | |
from twitter.api import Twitter | |
from twitter.oauth import OAuth, read_token_file | |
from twitter.oauth_dance import oauth_dance | |
# Yes, these are public secrets, published in sixohsix-twitter github repo. | |
CONSUMER_KEY='uS6hO2sV6tDKIOeVjhnFnQ' | |
CONSUMER_SECRET='MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk' | |
# Where OAuth credentials are saved, or where to save them. | |
oauth_filename = os.path.expanduser('~/.twitter_oauth') | |
# Create the OAuth credentials file if it doesn't already exist. | |
if not os.path.exists(oauth_filename): | |
# Reusing credentials from Python Twitter Tools. Consider creating your own. | |
oauth_dance("the Command-Line Tool", | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
oauth_filename) | |
# Read the local OAuth credentials file. | |
oauth_token, oauth_token_secret = read_token_file(oauth_filename) | |
# Create an instance of the twitter.api.Twitter class. | |
twitter = Twitter( | |
auth=OAuth( | |
oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET), | |
secure=True, | |
api_version='1', | |
domain='api.twitter.com') | |
# Congratulations, you now have an object mapped to the Twitter API. | |
# Now do something. | |
# Example: print most recent user status update (doesn't include "Retweets"). | |
timeline = twitter.statuses.user_timeline() | |
print timeline[0]['text'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment