Twitter Application

    • 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?
  1. Go to
  2. Sign in with your twitter account.
  3. Go to and click Create a new application button.
  4. The following page will load and you will fill the fields specified.


    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?
  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


  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

How to Authorize user?
  1. Download twitter OAuth library for PHP from following link and include in your code.

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

    $twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);

  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);

  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.
  5. Authorize URL is called to authorize currently logged in user to use your application.

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

  6. After Authorization, twitter will send oauth_verifier code.
  7. Now request for access tokens again using oauth_verifier

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

  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?
  1. Include twitter libraries in your code.
  2. Get user 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);

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

    $auth=$connection->get(‘’, array(‘oauth_token’ => oauth_token));

  4. For more details please visit.
In Simple Words
  1. Your App requests User permission.
  2. User approves (This is authorization)Using the $connection object now authenticate user using following code.

  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)

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

Related Posts