【问题标题】:Twitter OAuth unable to retrieve the access tokenTwitter OAuth 无法检索访问令牌
【发布时间】:2015-05-28 11:09:28
【问题描述】:

我正在用 PHP 构建一个 Twitter 应用程序,使用 TwitterOAuth 库进行用户身份验证。

我能够将用户重定向到 Twitter 进行身份验证并接收 oauth 令牌、oauth 密码和 oauth 验证程序,但是我无法完成获取访问令牌的身份验证的最后一步。 我正在本地主机上开发,我已经设置了回调路径

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

我的应用具有读写权限。

这是我的代码:

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');
        }
    ?>

执行时,update.php 中的 $access_token 变为

'{"error":"无效/过期 带有 HTTP 响应状态的令牌","re​​quest":"/oauth/access_token"}' 401 而不是返回身份验证值。

【问题讨论】:

    标签: php authentication twitter oauth


    【解决方案1】:

    事实证明,我的特定问题是由会话和回调 url 引起的。

    我通过 localhost/path/to/file 访问索引页面,但在用户身份验证后,twitter 重定向到 127.0.0.1/path/to/file,意味着存储在 localhost 上的会话数据在 127.0.0.1 上无法访问。

    使用 127.0.0.1 而不是 localhost 访问索引页解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多