Twitter API Consulting
140dev@gmail.com
781-879-2960
@140dev

Converting the 140dev Twitter Framework to use OAuth and API 1.1

by Adam Green on April 3, 2013

in Streaming API,Twitter OAuth

Twitter has announced that they will be turning off basic authentication for the Streaming API on May 7th. I’m preparing a new version of the 140dev Framework to use OAuth now, and will have it posted tomorrow. That will be good for new users of this code, but if you already have the code in production, I assume you have made changes to it. Here is a detailed set of instructions for modifying an existing installation to use OAuth. The good thing is that you only have to use single-user OAuth, which is pretty easy to implement. These notes should give you everything you need.

Obvious, but very IMPORTANT note: Make a complete backup of any current version of this code you now have in production before making these changes.

There are 5 steps you have to follow to convert an existing installation of this code to use OAuth with the streaming API version 1.1.

1. Create a Twitter application at http://dev.twitter.com, and get a complete set of tokens. If you haven’t created an app before, you will find detailed instruction in my free ebook.

You need 4 tokens as defined on the details page of the application settings:
Consumer Key
Consumer Secret
Access Token
Access Token Secret

It is traditional to rename these tokens in every library that uses them. This has happened again with the Phirehose OAuth library. Here are the new names:
Consumer Key = TWITTER_CONSUMER_KEY
Consumer Secret = TWITTER_CONSUMER_SECRET
Access Token = OAUTH_TOKEN
Access Token Secret = OAUTH_SECRET

2. Modify the 140dev_config.php file in the 140dev Framework code. This is found in /db/140dev_config.php.

You need to replace these lines of code:
// Basic authorization settings for connecting to the Twitter streaming API
// Fill in the values for a valid Twitter account
define(‘STREAM_ACCOUNT’, ‘*****’);
define(‘STREAM_PASSWORD’, ‘*****’);

With these new lines. Insert the actual tokens you received from Twitter in step 1:
// OAuth settings for connecting to the Twitter streaming API
// Fill in the values for a valid Twitter app
define(‘TWITTER_CONSUMER_KEY’,'******’);
define(‘TWITTER_CONSUMER_SECRET’,'******’);
define(‘OAUTH_TOKEN’,'******’);
define(‘OAUTH_SECRET’,'******’);

3. Get new copies of the Phirehose library code. You need to delete Phirehose.php in your current installation. It will be found in the /libraries/phirehose/ directory. Then download these files and place them in the /libraries/phirehose/ directory:
Phirehose.php
OauthPhirehose.php

4. The latest version of Phirehose.php still points to the 1.0 version of the streaming API. To point to version 1.1, you need to change line 20 of Phirehose.php from this:
const URL_BASE = ‘https://stream.twitter.com/1/statuses/’;

To this:
const URL_BASE = ‘https://stream.twitter.com/1.1/statuses/’;

5. The final step is modifying your copy of get_tweets.php, which is found in /db/get_tweets.php. There are two code changes:

First replace these lines starting at line 13:
// Extend the Phirehose class to capture tweets in the json_cache MySQL table
require_once(CODE_DIR . ‘libraries/phirehose/phirehose.php’);
class Consumer extends Phirehose

With these new lines:
require_once(CODE_DIR . ‘libraries/phirehose/Phirehose.php’);
require_once(CODE_DIR . ‘libraries/phirehose/OauthPhirehose.php’);
class Consumer extends OauthPhirehose

Then replace this line:
$stream = new Consumer(STREAM_ACCOUNT, STREAM_PASSWORD, Phirehose::METHOD_FILTER);

With this one:
$stream = new Consumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER);

Once you have made all these changes, you need to test out your code. Running the new code in the background with nohup is not an effective test method, since you can’t see the errror messages. To test the code, I would log into the directory with get_tweets.php and run it directly with:
php get_tweets.php

If it works, you will not see any error codes and data will flow into json_cache.php. Relive the thrill of when that first happened for you.

If it doesn’t work, there are a couple of things to try:
- Make sure all your paths are correct.
- Make sure you copied your OAuth tokens correctly into the config file.
- Make sure you downloaded a clean set of files from GitHub for Phirehose. It is very common to get HTML in the downloaded files by mistake.

If all else fails, I find cursing helps at this point. When you calm down, post a message at our Google Group, and I’ll try to help.

Previous post:

Next post: