Twitter oAuth的会话到期

时间:2012-10-12 23:18:43

标签: session twitter oauth

我正在使用Abraham Williams的oAuth库来更新状态。该应用程序没有UI(除了来自Twitter的凭据提示,而不是用户在浏览器中输入URL。

调用URL时,出现错误:“无法发布推文。错误:原因:1”。

我插入了一些测试代码,似乎会话在转换之间丢失:$ _SESSION ['tweetmsg']在index.php的初始调用中设置,但是当切换到connect.php时,似乎会话丢失了。有什么想法吗?

以下是源代码:

的index.php

<?php
include_once '../../winsinclude/tw_config.php';
require_once "../../winsinclude/twitteroauth.php";
require_once "../../winsinclude/OAuth.php";
session_start();
if (empty($_SESSION['access_token'])) {
  $_SESSION['tweetmsg'] = create_tweet_text();
  print "<script>self.location='./connect.php');</script>";
}

$connection = new TwitterOAuth(
  CONSUMER_KEY,
  CONSUMER_SECRET,
  $_SESSION['access_token']['oauth_token'],
  $_SESSION['access_token']['oauth_token_secret']
);

if (!isset($_SESSION['tweetmsg'])) {
  exit('No tweet value in session or from form');
}
$tweetmsg = $_SESSION['tweetmsg'];
$result = $connection->post('statuses/update', array('status' => $tweetmsg));
unset($_SESSION['tweetmsg']);
if (200 === $connection->http_code) {
  echo 'Tweet Posted: '.$tweetmsg;
}
else {
  echo 'Could not post Tweet. Error: '.$httpCode.' Reason: '.
  session_destroy();
}
function create_tweet_text () {
  return 'this is a test';
}

connect.php

?php
session_start();
include_once '../../winsinclude/tw_config.php';
require_once "../../winsinclude/twitteroauth.php";
require_once "../../winsinclude/OAuth.php";

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK.'callback.php');

$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

$url = $connection->getAuthorizeURL($request_token);
print "<script>self.location='$url';</script>";

callback.php

<?php
session_start();
include_once '../../winsinclude/tw_config.php';
require_once "../../winsinclude/twitteroauth.php";
require_once "../../winsinclude/OAuth.php";

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']);
  print "<script>self.location='index.php';</script>";
}

1 个答案:

答案 0 :(得分:0)

最近,Twitter停用了oAuth的一些http网址,并将其替换为https等价物。如果您在包含中看到网址字符串http://twitter.com/oauth/request_token,则表示您需要关注https://dev.twitter.com/discussions/10803并将所有通话更改为https ...