Getting started with Twitter API

For today we will have a play with the twitter API, see how we can get started using this popular platform.

Twitter is the poster boy of API adoption. Their own UI is built on top of their API and most of the clients it serves are actually API clients.

They provide a whole host of endpoints for us to play with including:

A more complete list can be found here https://dev.twitter.com/rest/public

With that said let us get started with their API

Steps creating an App

  1. Navigate to https://dev.twitter.com/apps
  2. Select Create new App and fill in the necessary details. For our tutorial you can simply use learningtwitternow in all fields
  3. Navigate to section named Keys and Access Tokens select Create my access token. Access tokens are what you will use in your application to access the API. Protect them as you would protect your password, they have the same power.

Using your app

All requests to Twitter API must be signed by Oauth. Thankfully they have provided us with a nice interface to carry out the authorization conversation.

To test our App click on Test Oauth button on top right

Test Oauth

Test Oauth

All the fields should be pre populated except request query and request URI.

Request URI is where we feed in our end point for example https://api.twitter.com/1.1/statuses/mentions_timeline.json The request query are any extra GET variables that need to be passed to the API. For now we have none of those.

Request URI

Request URI

We can now generate our signatures by clicking on Get Oauth Signature

Copy the curl command and paste it to your terminal. Should look something like this

 curl --get 'https://api.twitter.com/1.1/statuses/mentions_timeline.json'
 --header 'Authorization: OAuth oauth_consumer_key="1J4FmtUOB1QjznpNMLHPqxqdN", oauth_nonce="a391652ee21c00d7ef4b3c9d76d0143d", oauth_signature="cdl3G8XsUv1o4XlQnuzFbrlGLwM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1433834776", oauth_token="68480850-iV4oIN5uHXr0LNWPc5bPbhCNUJJQKZJ875nzmv1yJ", oauth_version="1.0"'
 --verbose

You probably don’t need the verbose switch and as such can omit the –verbose option.

Note this command will work for a few minutes at best before authorization is required once more.

For production environments, you may need use a library such as Abraham’s Twitter Oauth .

Have you used the twitter API in production? Let us know in comments below.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Published by

jchencha

Software Project Manager