Twitter返回[code] => 89我只是想第二次使用它

时间:2016-07-10 06:35:15

标签: php twitter twitter-oauth

您好我正在使用此代码获取oauth_tokenoauth_token_secret

require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
echo $url;
header('Location: '.$url);

在我的回电网址上是:

$request_token = [];
$request_token['oauth_token'] = $_REQUEST['oauth_token'];
$request_token['oauth_token_secret'] = 'JeVDKh0rSfBozrJ65p1lG4HaDmBtLkqF';
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_REQUEST['oauth_token'], $request_token['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials");
$add = "INSERT INTO users SET 
        name = '".$user->name."',
        username = '".$user->screen_name."',
        oauth_token = '".$_REQUEST['oauth_token']."',
        oauth_token_secret = '".$_SESSION['oauth_token_secret']."',
        oauth_verifier = '".$_REQUEST['oauth_verifier']."',
        id_string = '".$user->id_str."',
        status = '1' ";

但是当我尝试登录时从数据库中获取它后,它给了我

[code] => 89
[message] => Invalid or expired token.

在每个访问令牌上我都会收到此错误。我正在使用此代码进行重新登录

$oauth_token = 'token from db';
$oauth_token_secret = 'token_secrete from db';
$oauth_verifier = 'aoth verifier from db that i stored';
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret );
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier ));
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
$user = $connection->get("account/verify_credentials");

请帮我解决我的代码问题。我已经阅读过,不会使访问令牌过期。所以我目前的情况怎么了?

1 个答案:

答案 0 :(得分:0)

您不必在生成时手动分配access_token_secret。首先,在您的回调中,您可能希望添加一个表达式,以防万一URL中的oauth_token与您在执行请求时的会话中保存的oauth_token不一致。

if ($_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
    echo 'Oauth token does not match!';
    exit();
}

假设它们匹配,现在,创建与会话中存储的oauth_tokenoauth_token_secret的连接。

$connection = new TwitterOAuth($consumer_key, $consumer_secret,  $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

以上连接仅用于验证您的访问令牌。现在,使用回调网址参数中显示的$_REQUEST['oauth_verifier']进行验证。

$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

之后,您的访问令牌现已经过验证。您可以将其存储到会话(事先覆盖会话或创建新会话)或数据库存储。

//Verified access token
$oauth_token = $access_token['oauth_token'];
$oauth_token_secret = $access_token['oauth_token_secret'];

经过验证,现在你应该创建另一个连接来玩。就像你做的那样,查找帐户的凭据。

$newConnection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
$user = $newConnection->get("account/verify_credentials");

$add = "INSERT INTO users SET 
        name = '".$user->name."',
        username = '".$user->screen_name."',
        oauth_token = '".$oauth_token."',
        oauth_token_secret = '".$oauth_token_secret."',
        id_string = '".$user->id_str."',
        status = '1' ";

请注意,我删除了oauth_verifier,因为您不需要它。使用它只是为了验证未经验证的访问令牌,就像您第一次创建新请求一样。

最后,您可以按照上一个代码段从存储在数据库中的访问令牌打开另一个连接。

$oauth_token = $row['oauth_token_from_db'];
$oauth_token_secret = $row['oauth_token_secret_from_db'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);