Hi,
I am trying to use rest API 1.1 using Abraham’s PHP library. I am noticing that the oauth_token is changing value after authenticating with twitter and so I need to try to authenticate again. This happens only for the first attempt. Could you let me know what I am doing wrong ? Here is the code snippet/app flow.
- Flow starts in redirect.php with the following code:
session_start();
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION[‘oauth_token’] = $token = $request_token[‘oauth_token’];
$_SESSION[‘oauth_token_secret’] = $request_token[‘oauth_token_secret’];
/* If last connection failed don’t display authorization link. /
switch ($connection->http_code) {
case 200:
/ Build authorize URL and redirect user to Twitter. /
$url = $connection->getAuthorizeURL($token);
header('Location: ’ . $url);
break;
default:
/ Show notification if something went wrong. */
echo ‘Could not connect to Twitter. Refresh the page or try again later.’;
2). Control goes to https://api.twitter.com/oauth/authenticate site
3) Control goes to callback.php after going to twitter, Callback.php has the following code
/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST[‘oauth_token’]) && $_SESSION[‘oauth_token’] !== $_REQUEST[‘oauth_token’]) {
$_SESSION[‘oauth_status’] = ‘oldtoken’;
header(‘Location: ./clearsessions.php’);
}
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION[‘oauth_token’], $_SESSION[‘oauth_token_secret’]);
/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST[‘oauth_verifier’]);
/* Save the access tokens. Normally these would be saved in a database for future use. */
$_SESSION[‘access_token’] = $access_token;
/* Remove no longer needed request tokens */
unset($_SESSION[‘oauth_token’]);
unset($_SESSION[‘oauth_token_secret’]);
/* If HTTP response is 200 continue otherwise send to connect page to retry /
if (200 == $connection->http_code) {
/ The user has been verified and the access tokens can be saved for future use /
$_SESSION[‘status’] = ‘verified’;
header(‘Location: ./makerestapicalls.php’);
} else {
/ Save HTTP status for error dialog on connnect page.*/
header(‘Location: ./clearsessions.php’);
I am seeing that the condition $_SESSION[‘oauth_token’] != $_REQUEST[‘oauth_token’] at the top of callback.php is holding true ( twitter is returning a differentr value for [‘oauth_token’] after the authenticate). So instead of going to makerestapicalls.php where I actually do some calls, I need to authenticate again. Somehow the authenticate works the second time without issues.
Could you let me know why the ‘oauth_token’ value is changing, that too only for the first attempt?
Thanks,
Sankar