简体   繁体   中英

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. This works fine and I receive a Token for further usage. I can now query docs.google.com by using

        $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. But if I try the same query to spreadsheets.google.com with the URL obtained from the api documentation :

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

I get a 401 error saying that the token used is invalid. I am using the same token and query in both cases. Do I need a different token for the google spreadsheets 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. See here for more details - 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"
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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