簡體   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