PECL and twitter oauth

Currently, I am trying to make batbet to be able to communicate with twitter API. Twitter uses oauth as authentication method. The first thing comes in mind is to use liboauth for C (available here). In fact I am not so familiar and I am studying how twitter’s oauth works. I got clue from PECL sample (available here).

So, the first thing to do is how to make PECL works then port it to C. Why C? because this is the only language I know 😛

How to install PECL oauth extension?

$ pecl search oauth

Retrieving data…0%Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
oauth   0.99.9 (beta)   0.99.9 oauth consumer extension

then

$ sudo pecl install oauth-0.99.9

Now PECL oauth should be installed to your machine.

or you can also build manually the extension using phpize.

download oauth-0.99.9, extract then go inside oauth directory, invoke phpize

oauth-0.99.9 $ phpize

oauth-0.99.9 $ ./configure; make

oauth-0.99.9 $ sudo make install

To make your new extension works you should adjust your php.ini. On ubuntu there is /etc/php5/cli/php.ini then add following:

extension=oauth.so

Once you’re done, go to twitter example of oauth PECL package.

$ php updateStatus.php

you’ll see …

I was wondering how does the twitter authentication method go, from the beginning to the end. I might be wrong but it works for me, following is the workflow:

1. After you get consumer key and consumer secret, you need to request_token(). For instance, using PECL liboauth:

$request_token_info = $oauth->getRequestToken(https://twitter.com/oauth/request_token);

then you will get “oauth_token” reply.

2. You need to ask authorization from user, by giving a link to:

https://twitter.com/oauth/authorize?oauth_token=oauth_token -> you get this on the step #1

User will have to allow the access.

3. Right after authorization is granted by user, you need to request a new oauth token by accessing:

https://twitter/oauth/access_token

You will get oauth_token and oauth_token_secret to be used to call twitter REST API. This oauth_token and oauth_token_secret is persistent. I save these variables to a plaintext to be used as future request, so user does not need to do step #2 anymore.

4. Use Twitter REST API to do anything you want.

NOTE: I am open to any suggestion.

Advertisements

4 thoughts on “PECL and twitter oauth

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s