Using twitter authorization in Implementing twitter module for your website

Hello guys,
Before i started my big love story with web development i saw many websites are using twitter accounts for user login instead of registering new account. Actually i was impressed how those guys hacked twitter and got the accounts and passwords of the user and then check for the validity of these accounts 😀

Days and months later i got what’s happening behind the scenes. First you app sends the user email and password and asks twitter if this guy has a twitter account. Twitter then responds with “yes master he is a twitter user”, else twitter shows that he is not a registered twitter user. This process is called authorization process and that what actually happens. After the user successfully authorized, you can get his id and save it to your database. Each time user logins using his twitter account you check for the existence of that user.

Twitter uses OAuth protocol for authorization and there’s a simple library that implements that protocol in a simple way. Here’s what i’m gonna taking about https://github.com/abraham/twitteroauth

First you need to have a Twitter developer account, then create a twitter app to get the app secret and consumer key. You should specify the callback url because if you don’t do so it causes an big problem (specially for me :D). So don’t leave the callback url blanck.

I have written a simple code that helps you to authorize users through little snippet.

<?php
 
    include 'twitter_lib/OAuth.php';
    include 'twitter_lib/twitteroauth.php';
    include 'twitter_lib/config.php';
 
 
    /*  creating twitter object
     *  TwitterOAuth: @params : CONSUMER_KEY, CONSUMER_SECRET
     * 
     */
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
     
    /*  retrieving request tokens
     *  getRequestToken: @params : none
     *                   @return : request token array contains 'oauth_token'
     *                                                          'oauth_token_secret' 
     *                                                          'oauth_callback_confirmed'
     * 
     */
    $request_token = $connection->getRequestToken();
     
     
    /*  creating authorization url using twitter connection object
     *  getAuthorizeURL: @params : oauth token returned from the request token array
     *
     */
    $auth_url = $connection->getAuthorizeURL($request_token['oauth_token']);
     
    //redirect user to the authorization url
    header('Location: '.$auth_url);
     
     
?>

As you can see i have included three files at the top of my script ‘OAuth.php’ which is the core AOuth library, ‘twitteroauth.php’ which is the twitter implementation for AOuth and finally ‘config.php’ which holds your app configurations.

All you need to do is to open the ‘config.php’ and set the constants to your app consumer key and app consumer secret.

Here’s the full example source code download link.

http://www.mediafire.com/?p17z236k0qda4nl

Hope you have enjoyed, thanks 🙂