开发者

'Session expires' error in oauth for twitter

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_onc开发者_C百科e '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']);
}
?> 


callback.php is designed to be used once during each negotiation for an access token. Not each time you want to tweet.

I recommend breaking your code into the following three files:

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

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

index.php

<?php
session_start();

if (empty($_SESSION['access_token'])) {
  if (!empty($_POST['tweetmsg'])) {
    $_SESSION['tweetmsg'] = $_POST['tweet'];
  }
  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']
);

if (!empty($_POST['tweetmsg'])) {
  $tweetmsg = $_POST['tweetmsg'];
} elseif (!empty($_SESSION['tweetmsg'])) {
  $tweetmsg = $_SESSION['tweetmsg'];
} else {
  exit('No tweet value in session or from form');
}

$result = $connection->post('statuses/update', array('status' => $tweetmsg));
unset($_SESSION['tweetmsg']);
if (200 === $connection->http_code) {
  $resultmsg = 'Tweet Posted: '.$tweetmsg;
}
else {
  $resultmsg = 'Could not post Tweet. Error: '.$httpCode.' Reason: '.
  $result->error;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜