doubangzhang6992 2011-02-10 11:05
浏览 72

[PHP]使用google api oauth访问令牌时,错误401令牌无效

After getting acces token I'd like to use it to read protected data (e.g. contacts). But as a response to the request I'm getting
Token invalid - Invalid AuthSub token.
Error 401

I compared parameters that I am sending to those from OAuth Playground and there is no difference between them (except timestamp, nonce and signature which is understanding).
My headers looks like this:
Content-Type: application/atom+xml
Authorization: OAuth oauth_parameters

Any ideas what could go wrong?

  • 写回答

1条回答 默认 最新

  • dpwgzi7987 2011-05-27 19:08
    关注

    I wasn't setting the SESSION properly, so I was actually using my request token with the secret instead of my access token :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 - I would make sure that you have acquired 2 separate tokens (one for the initial request and then one for access). Another thing to take note of is it's important that you rebuild the new signature with the new token/secret because it was return "Invalid signature" if you keep it the same.

    Since there is a necessary redirection during this process you must keep the initial token secret in a SESSION variable. The oauth_token will be returned in the URL after the user is returned to your app, but the token secret will not. You must store it in a variable you can access after the round-trip is complete.

    If you wish to store the entire oauth_token object in a session make sure you instantiate the object BEFORE your "session_start()"

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

    Otherwise, you will receive a "incomplete object" warning when you reprint the token received from SESSION. I've spent a long time on this project, and have become deeply involved with OAuth. Please feel free to ask me any questions, Rafal.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行