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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码