Eric LeVan

Web Development

Simple Twitter API v1.1 Request Using TwitterOAuth

Jun 24, 2013

I am not a huge fan of social media and Twitter is no exception.  I've never used it for any real purpose and the only person worth following is George Takei.  But recently, a simple Twitter API v1 request which was made in an application I manage broke down due to the fact that all v1.1 API requests seem to require OAuth for authenticating request owners.

So where before I was able to make a HTTP request and parse the returning JSON for a profile feed, I now have to utilize information contained in my Twitter dev account to authenticate myself.  The data is public and could be retrieved with any screen scraper, but Twitter wants to track request sources.  I guess I can't blame them, but it is still an annoyance.

To solve the issue, I used TwitterOAuth by Abraham Williams, a nice PHP library for handling the minutia of authentication.  Here is the code I used to access a user's timeline:

// load oauth library require('twitteroauth/twitteroauth.php');
$consumer_key = '############';
$consumer_secret = '######################################';
$app_token = '################################################';
$app_token_secret = '################################################';

// do OAuth
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $app_token, $app_token_secret);
$token_credentials = new OAuthConsumer($app_token, $app_token_secret);
$data = $connection->get('statuses/user_timeline', array('screen_name' => 'JohnDoe'));

All we're really looking for here is to authenticate ourselves as a consumer using our credentials in a dev account.  Now we can fetch our data.

Say Something

Stuff People Said