dongquming3255 2015-05-28 09:06
浏览 28

访问PHP中的Google表格 - ClientLogin到OAuth

I have always used ClientLogin to access the Google Spreadsheets and from there made HTTP requests. Now that ClientLogin is deprecated I need to migrate to OAuth2, however I can't find any way of authenicating server-to-server without redirecting to some authentication page.

My PHP script runs in the background and before it did the following:

$url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
    "accountType" => "HOSTED_OR_GOOGLE",
    "Email" => $username,
    "Passwd" => $password,
    "service" => "wise",
    "source" => "SpreadsheetReader"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($status == 200) {
    if(stripos($response, "auth=") !== false) {
        preg_match("/auth=([a-z0-9_\-]+)/i", $response, $matches);
        $this->token = $matches[1];
    }
}

To read the spreadsheet, I would then make the following calls:

 $url = "https://spreadsheets.google.com/feeds/cells/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full";
 $headers = array(
      "Authorization: GoogleLogin auth=" . $this->token,
      "GData-Version: 3.0"
 );
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 $response = curl_exec($curl);

Essentially, I want to create the same effect but using OAuth2 - please can anyone help? Much appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改