Let's say you want to post to your Twitter account from a command-line python script. Here are the quick a dirty steps to get up and running with twython. There is another description at the twython site but it isn't as basic as this.
- Install twython with:
sudo pip install twython - Register the application on Twitter
- go to https://dev.twitter.com/apps
- fill out the form and hit "Create your Twitter application"
- From the App "Details" tab, click "modify app permissions" and change to "Read and Write" (it'll take a few minutes for that to be reflected on the app details page.
- Go get your API Keys
- Click on the "API Keys" tab from the Application Detail page
- Copy the API key
- In a terminal window:
- fire up the python interpreter
- import the modules you need:
from twython import Twython, TwythonError - make a variable for your API key:
APP_KEY =
and have it equal the thing you copied from the Twitter site using single quotation marks around it. - Switch back to the Twitter API Key browser page:
- Copy the API Secret key
- Switch back to terminal running the python interpreter:
- make a variable for your API secret:
APP_SECRET = and have it equal the thing you copied from the Twitter site using single quotation marks around it. - use twython to get a object by entering the following in the python interpreter:
twitter = Twython(APP_KEY, APP_SECRET) - then an auth object:
auth = twitter.get_authentication_tokens() - Still in the python interpreter, grab the oauth url copying the URL you see when you type:
auth['auth_url'] - Past the URL into a browser, sign into your account on Twitter if prompted and click "Authorize App." You should get a code, copy that code into your python interpreter window as a variable for the PIN
PIN = - create a variable with the AUTH TOKEN
AUTH_TOKEN=auth['oauth_token'] - create a variable with the AUTH SECRET
AUTH_SECRET=auth['oauth_token_secret'] - Then, in the interpreter, reconnect to Twitter and get the final authorization tokens with the PIN number that you set earlier:
twitter = Twython(APP_KEY, APP_SECRET, auth['oauth_token'], auth['oauth_token_secret'])
final_step = twitter.get_authorized_tokens(PIN) - then look at final_step['oauth_token'] and final_step['oauth_token_secret'] and save them somewhere in the configuration of your app. Also be sure to copy the original APP Key and APP Secret to a variable in your app. Below I use APP_KEY, APP_SECRET, OAUTH_TOKEN and OAUTH_TOKEN_SECRET
- Once you are all done you can use this code in an app to test it:
from twython import Twython, TwythonError
APP_KEY = [the thing you copied from earlier in single quotes]
APP_SECRET = [the thing you copied from earlier in single quotes]
OAUTH_TOKEN = [the thing you copied from earlier in single quotes]
OAUTH_TOKEN_SECRET = [the thing you copied from earlier in single quotes]
# Requires Authentication as of Twitter API v1.1
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
try:
twitter.update_status(status='Just a test, please ignore')
except TwythonError as e:
print e
PS I now use Tweepy instead of Twython but all of this is similar there.
3 comments:
Does this mean your posts on twitter are made by a bot? ;-)
Spam!
Spam!
Post a Comment