I am using abraham williams library to update twitter status using oauth. But I am constantly getting 'session expired' error. How can I get around this. This is my source.
connect.php
<?php
session_start();
require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "--------------------");
define("CONSUMER_SECRET", "---------------------------");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken('http://127.0.0.1/callback.php');
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] =
$request_token['oauth_token_secret'];
$url = $connection->getAuthorizeURL($request_token);
header('Location: ' . $url);
?>
index.php
<?php
session_start();
if (empty($_SESSION['access_token'])) {
header('Location: ./connect.php');
}
require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "---------------");
define("CONSUMER_SECRET", "--------------------------------");
$connection = new TwitterOAuth(
CONSUMER_KEY,
CONSUMER_SECRET,
$_SESSION['access_token']['oauth_token'],
$_SESSION['access_token']['oauth_token_secret']
);
include("form.php");
$msg = $_POST['tweet'];
if (!empty($msg)) {
$tweetmsg = $msg; }
else {
exit('Post a tweet');
}
$result = $connection->post('statuses/update', array('status' => $tweetmsg));
unset($_SESSION['tweetmsg']);
if (200 === $connection->http_code) {
echo'tweet posted';
}
else {
$resultmsg = 'Could not post Tweet. Error: '.$httpCode.'
Reason:'.$result->error;
echo $resultmsg;
}
?>
callback.php
<?php
session_start();
require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "---------------");
define("CONSUMER_SECRET", "------------------------------");
if (
isset($_REQUEST['oauth_token'])
&& $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']
) {
//echo 'Session expired';
header('Location: ./connect.php');
}
else {
$connection = new TwitterOAuth(
CONSUMER_KEY,
CONSUMER_SECRET,
$_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']
);
$_SESSION['access_token'] =
$connection->getAccessToken($_REQUEST['oauth_verifier']);
header('Location: index.php');
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
}
?>