PHPBuilder - Upgrading Basic Twitter Authentication to OAuth with PHP Page 2

RSS Twitter

Upgrading Basic Twitter Authentication to OAuth with PHP - Page 2

by: Sachin Khosla
June 25, 2010

Authenticating the User with Twitter OAuth

Now, if you look back at Figure 1, you will notice three steps for successfully authenticating the user. We will complete the steps here.
  1. Get the one-time request token and create the authorization link.
    $connection = new TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $connection->getRequestToken(CALLBACK_URL);
    //temporary store the request tokens in the session vars
    $_SESSION['oauth_token']  = $request_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
    //this function returns the authorization URL
    $authorize_url = $connection->getLoginURL($request_token);
    echo "<a href='$authorize_url'>Authorize this app</a>";
  2. The user authenticates the application and Twitter redirects the user to the CALLBACK_URL, which is defined in config.php.
    Now get the access token. Use the getAccessToken() function by using the verification (oauth_verifier), which the API appended with the callback URL. When you have the access token, you can play with the API however you want to. The following code shows how to update your status on Twitter.
    if($_SESSION['oauth_token'] !== $_REQUEST['oauth_token'])
    //token expired get a new one. You can clear session over here and redirect user to the login link
      die(token expired get a new one);
    $obj = new TwitterApi(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    $access_token = $obj->getAccessToken($_REQUEST['oauth_verifier']);
    $_SESSION['access_token'] = $access_token;
    unset ($_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] ,$obj);
    $obj = new TwitterApi(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    $content = $ obj ->doPost('', array('status' => 'This is my first update using OAuth.'));
    echo $connection->return_code;
    The above code will update your status on Twitter using OAuth.
The complete code is included below, and the README file will get you started. You can use the wrapper class included in the zip file and extend it to build your own applications.


If the code does not work, check the online console provided by Twitter to verify that the parameters you passed are correct. It shows you both the response and request.


Twitter OAuth will replace its basic authentication, so it is a good idea to migrate all your applications to the new code, if any. You can read about various updates related to the Twitter API on
Sachin Khosla is a Web developer and technology evangelist who has written and spoken extensively about open source technology. Sachin is part of an active open source community that organizes OSScamp in Delhi, India. To learn more about this author, read Sachin's blog at

« Previous Page

Comment and Contribute

Your comment has been submitted and is pending approval.

Sachin Khosla



(Maximum characters: 1200). You have characters left.