简体   繁体   English

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

[英]Login to Google Spreadshet API using ClientLogin and PHP/cURL

I am using the ClientLogin method and cURL to login to the google API's. 我正在使用ClientLogin方法和cURL登录到Google API。 This works fine and I receive a Token for further usage. 这可以正常工作,我会收到令牌以供进一步使用。 I can now query docs.google.com by using 我现在可以使用来查询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);

This works fine and I get a list of all the documents available in my google docs account. 这工作正常,我在我的google docs帐户中获得了所有可用文档的列表。 But if I try the same query to spreadsheets.google.com with the URL obtained from the api documentation : 但是,如果我尝试使用从api文档中获得的URL来对sheets.google.com进行相同的查询:

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

I get a 401 error saying that the token used is invalid. 我收到401错误,说使用的令牌无效。 I am using the same token and query in both cases. 我在两种情况下都使用相同的令牌和查询。 Do I need a different token for the google spreadsheets api? Google电子表格api是否需要其他令牌?

Edit: This is how I request the Token: 编辑:这是我请求令牌的方式:

        $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);

Short answer - Yes. 简短答案-是。 You need to generate different tokens for different services. 您需要为不同的服务生成不同的令牌。 The service names you pass in to retrieve an auh token are different in each case. 在每种情况下,您传递以获取auh令牌的服务名称都是不同的。 See here for more details - https://developers.google.com/gdata/faq 详情请参阅此处-https: //developers.google.com/gdata/faq

For example from the docs the req for spreadsheets would be 例如,从文档中,电子表格的要求为

$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