In my previous post, I mentioned about the twitter OAuth app hosted at: https://masnun.com/twitter-app/ . Here it comes, the source code.
I am not going to include the Twitter OAuth Library source. You have to download it from Github. The link is available in my previous post.
consumer.php
1 2 3 4 5 6 7 8 |
<?php // Filename: consumer.php // contains the consumer key and consumer secret // works as a configuration file that can be included in other // scripts. $consumer_key = 'Ll0Sq8HyJzBOqXqksH7N4w'; $consumer_secret = 'WwMdrN13UTJL79KyI6unm3xTm5tdF8E4U3FMKE7i5co'; ?> |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php // Filename: index.php // The file that gets called when you visit the app home page // require twitterOAuth lib require_once('twitterOAuth.php'); // require the consumer details -- consumer key and consumer secret require_once('consumer.php'); //construct a new twitter object $twitter = new TwitterOAuth($consumer_key,$consumer_secret); // get the token to make an authorization request $token = $twitter->getRequestToken(); // store the token for further use file_put_contents("token",$token['oauth_token']); file_put_contents("token_secret",$token['oauth_token_secret']); // build the authorization url $url = $twitter->getAuthorizeURL($token['oauth_token']); // redirect the user to the authorization url automatically header("Location: $url "); ?> |
return.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php //Filename: return.php // It's the callback file -- gets called // the user is returned to this file on authorization // include twitter OAuth library as usual include('twitterOAuth.php'); // include the consumer details include('consumer.php'); // retrieve the tokens $tok = file_get_contents("token"); $tok_sec = file_get_contents("token_secret"); // construct the twitter object $twitter = new TwitterOAuth($consumer_key,$consumer_secret,$tok,$tok_sec); // retrieve the access token set by the authorization $access_token = $twitter->getAccessToken(); // parse the tokens and seperate them $tok = $access_token['oauth_token']; $tok_secret = $access_token['oauth_token_secret']; // print_r($access_token); // construct the twitter object // this time with the access tokens $twitter = new TwitterOAuth($consumer_key,$consumer_secret,$tok,$tok_secret); // make API call $req = $twitter ->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => 'Check out: https://masnun.com/twitter-app/ :)'), 'POST'); // print_r($req); //redirect the user to twitter home page header("Location: http://twitter.com "); ?> |
Notes On OAuth:
First we, construct the twitter object with the consumer details.
Then we ask for a request token. We build the authorization url with this token.
We send the user to the auth URL.
When the user returns, again construct a twitter object with consumer details and the request token we retrieved previously.
Now that the user is authorized, we ask for access tokens.
We have the access tokens now, so build a new twitter object that has full access to the API.
Now, we can make API calls.
The steps may seem a bit odd at first look. Just go through a couple of times and you’ll get it clearly.
Enjoy OAuthentication, Enjoy Twitter enginnering !!