繁体   English   中英

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

这工作正常,我在我的google docs帐户中获得了所有可用文档的列表。 但是,如果我尝试使用从api文档中获得的URL来对sheets.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);

简短答案-是。 您需要为不同的服务生成不同的令牌。 在每种情况下,您传递以获取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"
        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM