【问题标题】:Google API 2 legged Oauth : "Token invalid - Invalid AuthSub token."Google API 2 有腿 Oauth:“令牌无效 - AuthSub 令牌无效。”
【发布时间】:2011-11-27 17:42:23
【问题描述】:

我的代码基于http://gdatatips.blogspot.com/2008/11/2-legged-oauth-in-php.html

这是我的代码,我想使用 Google Doc(文档列表)API:

        $endpoint = 'https://www.google.com/accounts/OAuthGetRequestToken';
        $consumer = new OAuthConsumer(GOOGLE_API_DOCLIST_CONSUMER_KEY, GOOGLE_API_DOCLIST_CONSUMER_SECRET, NULL);

        $arrParams = array(
                    'scope' => 'https://docs.google.com/feeds/'
                    ,'xoauth_requestor_id' => GOOGLE_API_DOCLIST_USER_EMAIL
                    );
        $req = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $endpoint, $arrParams);
        $req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);


        $curl = new Auth_Curl();
        $content = $curl->request($req->to_url());


        $docAPI = new GoogleAPI_DocumentList(new Auth_Curl(), $req->to_header());

        var_dump($req->to_header());
        echo '<br />-- Getting Collections list';

        $url = 'https://docs.google.com/feeds/default/private/full/-/folder' . '?xoauth_requestor_id=' . GOOGLE_API_DOCLIST_USER_EMAIL;
        $raw = $docAPI->getCollectionList($url);
        var_dump($raw);
        die();

我不断得到:

令牌无效 - AuthSub 令牌无效。

我做错了什么?

编辑: 这里有一些“提示”:

  • 它们似乎混合了 API 端点和基本提要。我已经为端点放置了 OAuthGetRequestToken。它似乎产生了有效的响应。
  • 我保留了它,但我不确定 xoauth_requestor_id 是必需的。
  • 文档告诉我们使用空格分隔 Authorization 标头中的参数。图书馆使用逗号。

【问题讨论】:

    标签: php oauth 2-legged


    【解决方案1】:

    这是更正后的代码:

        $endpoint = 'https://docs.google.com/feeds/default/private/full/-/folder';
        $consumer = new OAuthConsumer(GOOGLE_API_DOCLIST_CONSUMER_KEY, GOOGLE_API_DOCLIST_CONSUMER_SECRET, NULL);
    
        $arrParams = array(
                    'xoauth_requestor_id' => GOOGLE_API_DOCLIST_USER_EMAIL
                    );
        $req = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $endpoint, $arrParams);
        $req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
    
        $docAPI = new GoogleAPI_DocumentList(new Auth_Curl(), $req->to_header());
    
        var_dump($req->to_header());
        echo '<br />-- Getting Collections list';
    
        $url = 'https://docs.google.com/feeds/default/private/full/-/folder' . '?xoauth_requestor_id=' . GOOGLE_API_DOCLIST_USER_EMAIL;
        $raw = $docAPI->getCollectionList($url);
        var_dump($raw);
        die();
    
    • API 和 EndPoint 未混合。在 OAuth v1.0 上,您不要求服务器生成要在授权标头中使用的令牌,授权标头是 100% 本地生成的。 (我认为 URL 有时用于呈现授权标头)
    • 由于您不生成 access_token,因此无需定义范围。
    • 确保您有企业帐户
      • 只有企业帐户允许您激活 2 legged 身份验证。

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多