Saturday, September 18, 2010: I Just Want to Call Twitter's API With My Own Account! « from the old blog archive »

I'm not making a new Twitter client, or a Twitter service. I am not making an app for users to use. I just want to call the API with my account!

I think OAuth is complicated in some situations, especially when you want to do something very simple, e.g. posting a status update from your own account.

Before Twitter killed basic authentication, it was as simple as a single line curl command, but now, it's a lot more complicated.

Introducing OAuth Damnit

So let me introduce OAuth Damnit, a super simple Twitter OAuth library for PHP. This library is for you if:

  • You just want to make a simple PHP script that calls the Twitter API using your account.
  • You are not planning to let other users to sign in with your application.
  • You want to parse the response yourself.

This library is a very simple implementation of OAuth. It's only job is -- given the consumer key, consumer secret, access token, access token secret, and a GET/POST request to an OAuth service -- to sign the request, send the request to the server, and then pass the response from the server back.

I coded it based on abraham's twitteroauth project. Did not really copy any code from there, I just look at it to figure out how the OAuth process works.

Get OAuth Damnit

Get oauthdamnit.php here.

Registering an Application for Your Account

To make OAuth calls as an authenticated user, you will need a consumer key + secret and an access token + secret. To get that you will need to register a Twitter application.

First of all, you will want to create an application for yourself. Sign in with the Twitter account you want to use your application with, then go to http://dev.twitter.com/apps/new and register a new application.

  • Application Name is what people will see as the source of the tweet. On Twitter it will show as "less than 20 seconds ago via your application name"
  • Application Website: Put in your website address, so that when people click on your application's link, they will go to your website.
  • Application Type: Client.
  • Access Type: Read & Write.

Getting the Keys, Secrets and Tokens

After you register your application, you will be taken to your application's details page. Take note of your application's:

  • Consumer key
  • Consumer secret

Now on the right side, click the navigation link that says "My Access Token". Then take note of your:

  • Access token
  • Access token secret

Then make a new file to hold your key, token, and your secrets. Let's name it config.php.

Using OAuth Damnit

How to use it? Simple!

To create a new instance of the API object, you just:

$api = new OAuthDamnit(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

To GET something from the Twitter API:

$raw = $api->get($url, $params);

To POST something to the Twitter API:

$raw = $api->post($url, $params);

Where $url is the absolute URL to Twitter's API endpoint and $params is an array of parameters to send to Twitter API.

These 2 functions returns the raw response from Twitter's API and has not been processed in anyway. It's now up to you to do whatever you need to do with it.

An Example

This example calls verify_credentials method, parses it and displays the output.

An example of a POST request:

$raw = $api->post('http://api.twitter.com/1/statuses/update.json', array('status' => 'Testin it again!'));

That's It

Oh and if my library somehow doesn't work (it worked for me), please try to fix it and send me a correction. Thanks!