Twitter oAuth无效的请求令牌

时间:2015-02-09 14:28:33

标签: php twitter oauth

我使用TwitterOAuth PHP库将我的网页连接到Twitter,以检索经过身份验证的用户的数据,但它根本不起作用,我一直在尝试昨天,我还没弄明白怎么做。

这是我的代码:

    $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $credentials = $twitter->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
    $url = $twitter->url("oauth/authorize", array("oauth_token" => $credentials['oauth_token']));
    $_SESSION["oauth_token"] = $credentials["oauth_token"];
    $_SESSION["oauth_token_secret"] = $credentials["oauth_token_secret"];
    ?>
    <input type="button" name="contactTwitter" id="contactTwitter" onClick='window.location="<?php echo $url; ?>"' value="Connect to Twitter!"/>
    <?php
    if(isset($_GET['oauth_verifier'])) :
        $at=$twitter->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
        var_dump($at);
    endif;

它根本不起作用。我找到了很多解决TwitterOAuth问题的结果,但没有人帮助过我。我已经了解了Twitter API的功能 发送request_token,接收token_verifiers并获取access_tokens,但此处无效。
谢谢你的帮助。

1 个答案:

答案 0 :(得分:6)

问题解决了。
我使用另一个PHP文件来获取TwitterOAuth回调结果,该结果将access_token发送到我之前没有工作的页面。
如果有人需要这个,我发布了callback.php代码:

require_once('config.php');
session_start();
use Abraham\TwitterOAuth\TwitterOAuth;
$request_token = array();
$request_token['oauth_token'] = $_SESSION['oauth_token'];
$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

$connection = new TwitterOAuth(TW_CONSUMER_KEY, TW_CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

if (200 == $connection->lastHttpCode()) {
    $_SESSION['access_token'] = $access_token;
    unset($_SESSION['oauth_token']);
    unset($_SESSION['oauth_token_secret']);
    $_SESSION['status'] = 'verified';
} else {
    session_destroy();
}

header('Location:./reg.php?st=3');

然后,在注册页面中,我只是重复使用我之前找到的存储的$_SESSION['access_token']