drasv0904 2015-03-24 23:01
浏览 115
已采纳

Twitter OAuth无法检索访问令牌

I'm building a Twitter app with PHP using the TwitterOAuth library for user authentication.

I am able to redirect the user to Twitter for authentication and to receive the oauth token, oauth secret and oauth verifier, however I am not able to complete the last step of authentication where I get the access token. I'm developing on localhost and I have set up the callback path with

http://127.0.0.1:80/TwitterApp/update.php

My app has read and write permissions.

Here's my code:

index.php

<?php
    session_start();
    include "twitteroauth/autoload.php";
    include "oauthConsts.php";

    use Abraham\TwitterOAuth\TwitterOAuth;

    // request authentication tokens
    $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $oauth->getRequestToken(
                     'http://127.0.0.1/TwitterApp/update.php');

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

    if($oauth->getLastHttpCode()==200){
        // Let's generate the URL and redirect
        $url = $oauth->getAuthorizeURL($request_token['oauth_token']);
        header('Location: '. $url);
    } else {
        echo "something went wrong";
    }

?>

update.php

 <?php
        session_start();
        include "twitteroauth/autoload.php";
        include "oauthConsts.php";

        use Abraham\TwitterOAuth\TwitterOAuth;

        if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token'])
            && !empty($_SESSION['oauth_secret'])) {
            $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 
                         $_SESSION['oauth_token'], $_SESSION['oauth_secret']);
            $access_token = $oauth->oauth("oauth/access_token", 
                          array("oauth_verifier" => $_GET['oauth_verifier']));

            $_SESSION['access_token'] = $access_token;

        }
        else {
            header('Location: index.php');
        }
    ?>

On execution, $access_token in update.php becomes

'{"error":"Invalid / expired Token","request":"/oauth/access_token"}' with HTTP response status 401 instead of returning the authentication values.

  • 写回答

1条回答 默认 最新

  • douyao1856 2015-03-27 15:58
    关注

    As it turns out my particular issue was caused by sessions and the callback url.

    I was accessing the index page through localhost/path/to/file , but twitter was redirecting to 127.0.0.1/path/to/file after user authentication, meaning the session data stored on localhost was not accessible on 127.0.0.1.

    Using 127.0.0.1 rather than localhost to access the index page solved the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀