简体   繁体   English

Twitter oAuth无效的请求令牌

[英]Twitter oAuth Invalid request token

I'm using the TwitterOAuth PHP library to connect my web page to Twitter, to retrieve authenticated user's data, but it's not working at all, I've been trying since yesterday and I've still not figured out how to do it. 我正在使用TwitterOAuth PHP库将我的网页连接到Twitter,以检索经过身份验证的用户的数据,但它根本不起作用,我从昨天起就一直在努力,我仍然没有想出如何做到这一点。

This is my code: 这是我的代码:

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

And it's not working at all. 而且根本不起作用。 I've found lots of results to solve TwitterOAuth issues, but no one has helped me. 我找到了许多解决TwitterOAuth问题的结果,但没有人帮助过我。 I've understood Twitter API's functioning like sending request_token , receiving token_verifiers and obtaining access_tokens but it's not working here. 我已经了解Twitter API的功能,如发送request_token ,接收token_verifiers和获取access_tokens但它在这里不起作用。
Thanks for help. 感谢帮助。

Problem solved. 问题解决了。
I used another PHP file to get the TwitterOAuth callback result, which sends the access_token to my previous page which wasn't working. 我使用另一个PHP文件来获取TwitterOAuth回调结果,该结果将access_token发送到我以前的页面,该页面无效。
In case that someone will need this, I'm posting the callback.php code: 如果有人需要这个,我发布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');

Than, in the registration page i just re-use the stored $_SESSION['access_token'] which I've found before. 在注册页面中,我只是重新使用我之前找到的存储的$_SESSION['access_token']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM