Setting up OAuth for Twitter API

Posted on Updated on

1. On logging in to twitter’s developers’ website (https://dev.twitter.com/apps) and creating an app, I received the key and secret as follows:

Access level Read, write, and direct messages
Consumer key nIEpbVLzcEJpCmj0HuVY0A
Consumer secret HcofB6Np6xqmsDeCiJLKP5REiOBb6onpaWebAtGPc
Request token URL https://api.twitter.com/oauth/request_token
Authorize URL https://api.twitter.com/oauth/authorize
Access token URL https://api.twitter.com/oauth/access_token
Callback URL http://127.0.0.1:8000/twitter_callback
Sign in with Twitter No

2. Running the script by opening the twitterAuthTest.php file on browser.

[http://www.rashmiumd.altervista.org/twitter2/twitterAuthTest.php]
It displays all my tweets in the following format:

name (time stamp) <line break> tweet

Eg:

 Rashmi Sankepally (Tue Nov 26 19:32:44 +0000 2013)

Welcome to rashmiumd’s Portal | rashmiumd’s Portal http://t.co/xE5FGAGYzc

3. Commenting out the statements that set the Authorization Token and Authorization Secret gives the following error:

Fatal error: Uncaught exception ‘Exception’ with message ‘Could not authenticate you’ in /membri/rashmiumd/twitter2/Twitter.php:509 Stack trace: #0 /membri/rashmiumd/twitter2/Twitter.php(797): Twitter->doCall(‘statuses/user_t…’, NULL) #1 /membri/rashmiumd/twitter2/twitterAuthTest.php(14): Twitter->statusesUserTimeline() #2 {main} thrown in /membri/rashmiumd/twitter2/Twitter.php on line 509

4. Even when the authorization key is not valid, the same error is reported:

Fatal error: Uncaught exception ‘Exception’ with message ‘Could not authenticate you’ in /membri/rashmiumd/twitter2/Twitter.php:509 Stack trace: #0 /membri/rashmiumd/twitter2/Twitter.php(797): Twitter->doCall(‘statuses/user_t…’, NULL) #1 /membri/rashmiumd/twitter2/twitterAuthTest.php(14): Twitter->statusesUserTimeline() #2 {main} thrown in /membri/rashmiumd/twitter2/Twitter.php on line 509

5. On running the script – twitterAuthTest2.php file on browser, the following things happen:

It redirects me to: https://api.twitter.com/oauth/authorize?oauth_token=QhoINbboRv1LG0jzBqxQJz6ViOPw2zq3re7Szg7PLKs and asks me for authorization

Capture_twitter

Each time I open twitterAuthTest2, it uses a new oauth_token in the address bar.

After I logged in and authorized it, it redirects to:

http://www.rashmiumd.altervista.org/twitter2/twitterAuthTest2.php?oauth_token=QhoINbboRv1LG0jzBqxQJz6ViOPw2zq3re7Szg7PLKs&oauth_verifier=MU0oWeN3io4rTaDYn78o4BsGaBw2IfoDrsGLL1IKQ

So on authorizing, it automatically generates a oauth_verifier on the fly without having to put it in the code.

On authorizing, again the page with all my tweets is displayed as in part 2.

Advertisements

One thought on “Setting up OAuth for Twitter API

    SutoCom said:
    December 16, 2013 at 12:49 pm

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