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

Mar*_*rco 5 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);
Run Code Online (Sandbox Code Playgroud)

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

https://spreadsheets.google.com/feeds/spreadsheets/private/full
Run Code Online (Sandbox Code Playgroud)

我收到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);
Run Code Online (Sandbox Code Playgroud)

Esw*_*ala 6

简短回答 - 是的.您需要为不同的服务生成不同的令牌.您传入的用于检索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"
        );
Run Code Online (Sandbox Code Playgroud)