How To Create a Twitter Application – Authorization and Authentication

Twitter Application

Synopsis :

  • How to create a twitter application.
  • How to get OAuth tokens.
  • How to authorize users.
  • How to authenticate users whenever he logs in.
  • In Simple Words

 

How to create a Twitter Application?

Step-1 : Go to http://dev.twitter.com

Step-2 : Sign in with your twitter account.

Step-3 : Go to https://dev.twitter.com/apps and click “Create a new application” button.

Step-4 : The following page will load and you will fill the fields specified.

form

Name -> Name of your App.

Description -> Description of your app.

Website -> Site where this app will be available or where info of this app will be available.

Callback URL(Optional) -> This is the URL where the user will be redirected after he authorizes your app.

 

How to get OAuth tokens?

Step 1: After creating application you can see details of your app. There in “OAuth Settings” menu Consumer Key and Consumer Secret Key is available. Also know as Access key and Access Secret

keys

Step-2 : Note down the both keys and save it. Never let anyone know your consumer key or secret except you, it may lead to misuse of your app.

 

How to Authorize user?

Step-1 : Download twitter OAuth library for PHP from following link and include in your code.

https://github.com/abraham/twitteroauth

Step-2 : Create a TwitterOauth object using your consumer key and secret key.

                $twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);

Step-3 : Request Access tokens for the app. Specify your callback URL, if you left it blank while creating app.

                $request_token = $twitteroauth->getRequestToken(YOUR_CALLBACK_URL);

Step-4 : User logs in and approves your app and twitter returns with “oauth_token” and “oauth_token_secret”. Please be aware that these tokens are different from your consumer token and secret.

Step-5 : Authorize URL is called to authorize currently logged in user to use your application.

                $url = $twitteroauth->getAuthorizeURL(oauth_token);

Step-6 : After Authorization, twitter will send oauth_verifier code.

Step-7 : Now request for access tokens again using oauth_verifier

                $access_token = $twitteroauth->getAccessToken(oauth_verifier);

Step-8 : Now you will get permanent oauth_token and oauth_secret for the user. Which you can save in database and user need not authorize the app everytime.

 

How to Authenticate user whenever he logs in?

Step-1 : Include twitter libraries in your code.

Step-2 : Get user’s oauth_token and oauth_secret which is stored in database and use following code to get twitter oauth object.

                $connection= new TwitterOAuth(CONSUMER_KEYCONSUMER_SECRET, oauth_token, oauth_token_secret);

Step-3 : Using the $connection object now authenticate user using following code.

                $auth=$connection->get(‘https://api.twitter.com/oauth/authenticate’, array(‘oauth_token’ => oauth_token));

For more details please visit. https://dev.twitter.com/docs/api/1/get/oauth/authenticate

 

In Simple Words

Step 1 : Your App requests User permission.

Step 2 : User approves (This is authorization)

Step 3 : From the moment user approves your app, his account is accessible by your app without his login credentials or his permission. (This is authentication)

Step 4 : Your app can then do all the twitter work (tweet, retweet, follow, unfollow etc..) behalf of the user.