Implement Login With Twitter Button

Implementing login using Twitter directly through their provided API endpoints can be tedious and require knowledge of external systems like the OAuth protocol. There are some useful libraries that can help you to quickly and easily implement a "Login using Twitter" button and get your users tweeting through your site. Below we go through the steps to setup a common PHP library used to handle Twitter social login authentication and functionality. Twitter has information on many common technologies libraries here. You can view a sample implementation with many different features that rely on login using Twitter here.

1. Begin by downloading the Twitteroauth package which we will be using to setup the login using Twitter button. You can use the following composer package to get this:

{<br>"require": {
"php": ">=5.4.0", 
"abraham/twitteroauth": "0.3.0-beta" 

2. Import the TwitterOAuth class onto your page and initialize the connection:

require 'vendor/autoload.php'; <br>use Abraham\TwitterOAuth\TwitterOAuth; 
define('CONSUMER_KEY', ); 
define('CONSUMER_SECRET', ); 
define('OAUTH_CALLBACK', ); 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

3. Create a Twitter App on the Twitter dev site that will service the requests for Twitter social login for your site. Replace the above definitions with the following details from your App:

  • The Consumer Key found in the created app found under Keys and Access Tokens.
  • The Consumer Secret found in the created app found under Keys and Access Tokens.
  • The URL that you would like a user to return to after successfully authenticating.

4. Next generate a Request token that will be used to generate a URL which will trigger the twitter Authentication popup this action can be triggered by an event such as the onclick of a button.

$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));

5. From the response object capture the "oauth_token" and "oauth_token_secret" for further use. Generate the URL to be used to display the Twitter social login Authentication interface.

$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));

6. Setup a button or trigger to cause users to navigate to this URL which will display the Twitter social login interface. Once a user has successfully authenticated they will be redirect to the location that you specified in the request_token OAUTH_CALLBACK URL.On your Callback page import the TwitterOAuth class again:

require 'vendor/autoload.php';<br>use Abraham\TwitterOAuth\TwitterOAuth; 
define('CONSUMER_KEY', );
define('CONSUMER_SECRET', ); 
define('OAUTH_CALLBACK', );

7. Define the definitions as you did in step 2. Initialize the connection using the stored "oauth_token" and "oauth_token_secret".

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 'oauth_token', 'oauth_token_secret');

8. Finally request an access_token to be used to access user data and actions.

$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

At this point your user is logged in and you can make calls to Twitter social login APIs and you have successfully set login using Twitter on your page. Want to extend your social systems with additional provider functionality check out this post on LinkedIn social login or want to see additional examples of Twitter social Login check out our demo page here.



  • Epilog X Contest

    Epilog X Contest
  • Pocket Sized Contest

    Pocket Sized Contest
  • Organization Contest

    Organization Contest



3 years ago

What happens? It should trigger a Twitter authentication interface where the user can input there Handle/email and password.