Quick & Dirty For Setting Up Using Twitter in Python

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.

  1. Install twython with:
    sudo pip install twython
  2. Register the application on Twitter
    1. go to https://dev.twitter.com/apps
    2. fill out the form and hit "Create your Twitter application"
  3. 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.
  4. Go get your API Keys
    1. Click on the "API Keys" tab from the Application Detail page
    2. Copy the API key
  5. In a terminal window:
    1. fire up the python interpreter
    2. import the modules you need:
      from twython import Twython, TwythonError
    3. 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.
  6. Switch back to the Twitter API Key browser page:
    1. Copy the API Secret key
  7. Switch back to terminal running the python interpreter:
    1. 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. 
    2. use twython to get a object by entering the following in the python interpreter:
      twitter
      = Twython(APP_KEY, APP_SECRET)
    3. then an auth object:
      auth = twitter.get_authentication_tokens()
  8. Still in the python interpreter, grab the oauth url copying the URL you see when you type:
    auth['auth_url']
  9. 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 = 
    1. create a variable with the AUTH TOKEN
      AUTH_TOKEN=auth['oauth_token']
    2. create a variable with the AUTH SECRET
      AUTH_SECRET=auth['oauth_token_secret']
  10. 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)
  11. 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
  12. 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:

Amit said...

Does this mean your posts on twitter are made by a bot? ;-)

Amit said...

Spam!

Amit said...

Spam!