对于Twitter,oauth中的“会话过期”错误

时间:2010-09-18 14:50:24

标签: session twitter oauth

我正在使用亚伯拉罕威廉姆斯图书馆来更新推特状态 的OAuth。但我不断得到:

  

会话已过期

错误。我怎么能绕过这个?

这是我的来源:

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

1 个答案:

答案 0 :(得分:2)

callback.php旨在在每次协商期间使用一次访问令牌。不是每次都想发推文。

我建议将代码分成以下三个文件:

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