You can use twitter login in your websites to allow users to login using their twitter account.you don’t need an extra registration and user management for your sites just like facebook login .This tutorial will guide you to Login With Twitter using OAuth. I have used Twitter OAuth PHP library for making OAuth requests.

Steps for Twitter OAuth Login

Get Twitter API Consumer Key and Consumer Secret.

Step 1 :
  • Name: Your application Name. This is shown to user while authorizing.
  • Description: Your application Description.This is shown to user while authorizing.
  • Website: Your application website.
  • Callback URL(*): After authorization, this URL is called with oauth_token.
Step 2 :

After creating app, you can see “Consumer Key” and “Consumer Secret”.

Step 3:

Download the twitter developer api for php from https://dev.twitter.com/overview/api/twitter-librariesor Download the fully configured filelogin-with-twitter

Step 4:

Include the twitteroauth.php file in order to pass the credentials. Get a Request token from twitter (login.php) getRequestToken($OAUTH_CALLBACK); //get Request Token if($request_token) { $token = $request_token['oauth_token']; $_SESSION['request_token'] = $token ; $_SESSION['request_token_secret'] = $request_token['oauth_token_secret']; switch ($connection->http_code) { case 200: $url = $connection->getAuthorizeURL($token); //redirect to Twitter . header('Location: ' . $url); break; default: echo "Connection with twitter Failed"; break; } }else //error receiving request token {echo "Error Receiving Request Token"; }? > Get access_token using request_token and request_token_secret. After getting access token, we can query Twitter API.

Step 5:

Use https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials API to validate the user. (create a oauth.php. Your callback File) < ? php if(isset($_GET['oauth_token'])) { $connection = new TwitterOAuth($CONSUMER_KEY', $CONSUMER_SECRET,$request_token,request_token_secret); $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); if($access_token) { $connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); $params =array(); $params['include_entities']='false'; $content = $connection->get('account/verify_credentials',$params); if($content && isset($content->screen_name) && isset($content->name)) { $_SESSION['twitter_name']=$content->name; $_SESSION['twitter_id']=$content->id; header('Location:'. $_SESSION['twitter_url']);} else {echo "Login Error";} }else{echo "Login Error";} }}? >

Step 6:

Now when you login through the twitter a dialog box like below will be displayed(if you are already logged-in else first login to twitter )

Leave a Reply

Your email address will not be published. Required fields are marked *