使用客户端登录和PHP / cURL登录Google Spreadsheet API

时间:2012-06-24 08:46:04

标签: php curl google-docs-api google-spreadsheet-api

我正在使用ClientLogin方法和cURL登录google API。这工作正常,我收到一个令牌供进一步使用。我现在可以使用

查询docs.google.com
        $curl = curl_init();

        $headers = array(
            "Authorization: GoogleLogin auth=" . $auth,
            "GData-Version: 3.0",
        );

        curl_setopt($curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full");
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($curl);
        curl_close($curl);

这很好用,我得到了谷歌文档帐户中所有可用文档的列表。但是,如果我使用从api documentation获得的网址对spreadsheets.google.com进行相同的查询:

https://spreadsheets.google.com/feeds/spreadsheets/private/full

我收到401错误,说明使用的令牌无效。我在两种情况下都使用相同的标记和查询。我是否需要为Google电子表格api提供不同的令牌?

编辑: 这是我请求令牌的方式:

        $clientlogin_url = "https://www.google.com/accounts/ClientLogin";
        $clientlogin_post = array(
            "accountType" => "HOSTED_OR_GOOGLE",
            "Email" => "my email",
            "Passwd" => "my password",
            "service" => "writely",
            "source" => "my application name"
        );

        $curl = curl_init($clientlogin_url);

        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($curl);

        preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
        $auth = $matches[1];
        curl_close($curl);

1 个答案:

答案 0 :(得分:6)

简短回答 - 是的。您需要为不同的服务生成不同的令牌。您传入的用于检索auh令牌的服务名称在每种情况下都是不同的。有关详细信息,请参阅此处 - https://developers.google.com/gdata/faq

例如,从文档中,电子表格的req将是

$clientlogin_post = array(
            "accountType" => "HOSTED_OR_GOOGLE",
            "Email" => "my email",
            "Passwd" => "my password",
            "service" => "wise",
            "source" => "my application name"
        );