【问题标题】:[PHP]Error 401 Token invalid while using google api oauth access token[PHP]使用 google api oauth 访问令牌时出现错误 401 令牌无效
【发布时间】:2011-05-28 03:24:33
【问题描述】:

获得访问令牌后,我想用它来读取受保护的数据(例如联系人)。但作为对请求的回应,我收到了
Token invalid - Invalid AuthSub token.
Error 401

我比较了我发送的参数和来自 OAuth Playground 的参数,它们之间没有区别(时间戳、随机数和签名除外)。
我的标题如下所示:
Content-Type: application/atom+xml
Authorization: OAuth oauth_parameters

有什么想法会出错吗?

【问题讨论】:

  • 我遇到了完全相同的错误(Google Analytics API 除外)。有什么建议吗?

标签: php api oauth http-status-code-401


【解决方案1】:

我没有正确设置 SESSION,所以我实际上是在使用我的请求令牌和秘密而不是我的访问令牌:P

    if(empty($_GET["oauth_token"])) { 
    $_SESSION["token_secret"] = $oauthTokenSecret;
    $_SESSION["oauth_token"] = $oauth_token;

    echo '<script>window.location="'.$auth_url.'";</script>';
}
else {

    $oauth_token->key = urldecode($_GET["oauth_token"]);
    $oauth_token->secret = $_SESSION["token_secret"];

    echo "<Br>";
    echo "<Br> FIRST OAUTH TOKEN: ";
    echo $oauth_token->key;

    echo "<Br>";
    echo "<Br> FIRST OAUTH TOKEN SECRET: ";
    echo $oauth_token->secret;

    print_r($_SESSION);
    echo "<Br>";
    echo "<Br>";

    // GET https://www.google.com/accounts/OAuthGetAccessToken
    $req = OAuthRequest::from_consumer_and_token($consumer, $oauth_token, 'GET',
        $token_endpoint.'OAuthGetAccessToken', array('oauth_verifier' => $_GET['oauth_verifier']));

    $req->sign_request($sig_method, $consumer, $oauth_token, $privKey);

    print_r($req); 

    $response = send_signed_request('GET', $token_endpoint.'OAuthGetAccessToken', array($req->to_header()));

@Rafal - 我会确保您获得了 2 个单独的令牌(一个用于初始请求,一个用于访问)。需要注意的另一件事是,使用新令牌/秘密重建新签名很重要,因为如果保持不变,它将返回“无效签名”。

由于在此过程中需要重定向,因此您必须将初始令牌保密在 SESSION 变量中。 oauth_token 将在用户返回到您的应用程序后的 URL 中返回,但令牌密钥不会。您必须将其存储在往返完成后可以访问的变量中。

如果您希望将整个 oauth_token 对象存储在会话中,请确保在“session_start()”之前实例化该对象

$oauth_token = new OAuthToken($oauthToken, $oauthTokenSecret);

session_start();

否则,当您重新打印从 SESSION 收到的令牌时,您将收到“不完整的对象”警告。我在这个项目上花了很长时间,并且深入参与了 OAuth。如有任何问题,请随时问我,Rafal。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2014-02-15
    • 1970-01-01
    • 2014-04-18
    • 2019-04-28
    • 1970-01-01
    • 2011-11-27
    • 2018-10-28
    相关资源
    最近更新 更多