Php Twitter OAuth-无效或过期的访问令牌
我正在尝试使用最新版本的来分享帖子。下面是我的代码Php Twitter OAuth-无效或过期的访问令牌,php,cakephp-2.0,twitter-oauth,Php,Cakephp 2.0,Twitter Oauth,我正在尝试使用最新版本的来分享帖子。下面是我的代码 require APP."Vendor/twitteroauth-master/autoload.php"; // twitter v0.5.1 SDK if(isset($_REQUEST['oauth_verifier'])) { $request_token = []; $request_token['oauth_token'] = $this->Session->read('twtr_oa
require APP."Vendor/twitteroauth-master/autoload.php"; // twitter v0.5.1 SDK
if(isset($_REQUEST['oauth_verifier'])) {
$request_token = [];
$request_token['oauth_token'] = $this->Session->read('twtr_oauth_token');
$request_token['oauth_token_secret'] = $this->Session->read('twtr_oauth_token_secret');
/* If denied, bail. */
if (isset($_REQUEST['denied'])) {
exit('Permission was denied. Please start over.');
}
/* If the oauth_token is not what we expect, bail. */
if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) {
$this->Session->write("twtr_oauth_token" , '');
$this->Session->write("twtr_oauth_token_secret" , '');
exit;
}
debug($request_token);
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth("KEY", "SECRET", $request_token['oauth_token'], $request_token['oauth_token_secret']);
debug($connection);
$content = $connection->get('account/verify_credentials');
debug($content);
/* Request access token from twitter */
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
/** Finally post things */
$result = $connection->post("statuses/update", array("status" => "hello world"));
if(isset($result->errors) && count($result->errors) > 0) {
debug($connection->post("statuses/update", array("status" => "hello world")));
exit;
}
$this->redirect("http://twitter.com");
} else {
$connection = new TwitterOAuth("KEY", "SECRET");
/** Get Temp Token */
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => "https://180.211.99.162:9051/widgetapi/twitterOauth"));
switch ($connection->getLastHttpCode()) {
case 200:
/** Write OAuth token and secret into session */
$this->Session->write("twtr_oauth_token" , $request_token['oauth_token']);
$this->Session->write("twtr_oauth_token_secret" , $request_token['oauth_token_secret']);
/** Build Auth URL */
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
/** Redirect */
$this->redirect($url);
break;
default:
/* Show notification if something went wrong. */
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
}
当我试图实现这一点时,我会得到无效或过期的访问令牌
调试信息
/* Request access token from twitter */
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
$connection = new TwitterOAuth("KEY", "SECRET", $access_token['oauth_token'], $access_token['oauth_token_secret']);
我搞不懂这个问题!知道这里出了什么问题吗?一旦得到回调,用新的访问令牌再次初始化类
/* Request access token from twitter */
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
$connection = new TwitterOAuth("KEY", "SECRET", $access_token['oauth_token'], $access_token['oauth_token_secret']);
使用最新版本的,在@user3989103的帮助下,在这个线程和中,对我有效的是:
$connection = new TwitterOAuth($key, $secret, $_SESSION['request_token'], $_SESSION['request_token_secret']);
$access_token = $connection->oauth('oauth/access_token', array('oauth_verifier' => $_REQUEST['oauth_verifier'], 'oauth_token'=> $_REQUEST['oauth_token']));
$connection = new TwitterOAuth($key, $secret, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$credentials = $connection->get('account/verify_credentials');
对于我,我也给了oauth_代币。为了使它在$access\u-token=$connection->oauth(“oauth/access\u-token”,数组(“oauth\u-verifier”=>$\u-REQUEST['oauth\u-verifier'],“oauth\u-token”=>$oauth\u-token))行中工作;考虑到这个库上没有文档,用它来计算承载令牌是一个噩梦。多亏了这个答案,我才得以解决我的问题。谢谢