使用 ClientLogin 和 PHP/cURL 登录 Google Spreadshet API


Login to Google Spreadshet API using ClientLogin and PHP/cURL

我正在使用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);

这工作正常,我得到了我的谷歌文档帐户中所有可用文档的列表。但是,如果我尝试使用相同的查询来 spreadsheets.google.com 从 api 文档中获得的 URL:

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

我收到一个 401 错误,指出使用的令牌无效。在这两种情况下,我都使用相同的令牌和查询。我是否需要为谷歌电子表格 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);

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

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

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