Login With Twitter Tutorial

In this tutorial,I have covered how to Login With Twitter using OAuth. I have used Twitter OAuth PHP library for making OAuth requests.

How Twitter OAuth Works

Login With Twitter OAuth Flow

1) “Login With Twitter” button is shown to users. (index.php)
2) When a user clicks on “Login with Twitter”, user is redirected to Twitter. (login.php)
3) Twitter verifies the user credentials.
4) After successful authorization , user is redirected to website with OAuth token. (oauth.php)
5) Using OAuth token, website gets the access token. (oauth.php)
6) Using Access token, website gets the user info by making a API call.(oauth.php)

Login With Twitter DemoDownload Login with Twitter Source


You need to follow the below steps for making “Login with Twitter”

Get Twitter API Consumer Key and Consumer Secret.

1) Login to twitter and access https://dev.twitter.com/apps/new. Fill the below details and create app:

  • 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

Create Twitter App

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

Cosumer Key and Secret



How to query Twitter OAuth API using PHP:

Once your are ready with Consumer Key and Consumer Secret, you can make OAuth API calls.

1) Configure your app (config.php)





2) Get a Request token from twitter (login.php)


$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET);
$request_token = $connection->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); 
		    echo "Coonection with twitter Failed";

else //error receiving request token
	echo "Error Receiving Request Token";


3) Get access_token using request_token and request_token_secret. After getting access token, we can query Twitter API.
Use https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials API to validate the user. (oauth.php)


//oauth.php. Your callback File


	$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $_SESSION['request_token'], $_SESSION['request_token_secret']);
	$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
		$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
		$params =array();
		$content = $connection->get('account/verify_credentials',$params);

		if($content && isset($content->screen_name) && isset($content->name))

			//redirect to main page. Your own
			header('Location: login.php'); 

		       echo "<h4> Login Error </h4>";


	echo "<h4> Login Error </h4>";

About Author

I am a developer and I maintain the site https://hayageek.com. The best software developers are those who can think like both a developer and a user.
All posts by Ravishanker Kusuma