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

时间:2015-05-28 09:06:23

标签: php authentication oauth oauth-2.0 google-api

我一直使用ClientLogin访问Google Spreadsheets并从那里发出HTTP请求。既然不推荐使用ClientLogin,我需要迁移到OAuth2,但是我找不到任何方法来验证服务器到服务器,而无需重定向到某个身份验证页面。

我的PHP脚本在后台运行,然后执行以下操作:

$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];
    }
}

要阅读电子表格,我会进行以下调用:

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

基本上,我想创建相同的效果,但使用OAuth2 - 请有人帮忙吗?非常感谢。

0 个答案:

没有答案