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 đ