是否可以通过 Google API 服务帐户访问 Google 电子表格 API?如何在 PHP 中执行此操作?

Is it possible to access the Google Spreadsheets API via an Google API Service account? How to do so in PHP?

我成功地使用 Google PHP API 和一个服务帐户来操纵 Google CloudDNS 记录,如下所示:

$permisson = 'https://www.googleapis.com/auth/ndev.clouddns.readwrite';

$client = new Google_Client();
$client->setApplicationName("foo");

$credentials = new Google_Auth_AssertionCredentials(
    $googleApiServiceAccount,
    array($permission),
    file_get_contents($googleApiKeyFile)
);

$client->setAssertionCredentials($credentials);

if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($credentials);
}

$accessToken = $client->getAccessToken();

[..]

是否可以对 Google Apps 电子表格访问使用类似的方案?我必须在 Google 开发者控制台中启用什么作为 $permission 以及什么样的 API?

请注意:我完全知道通过 oAuth2 访问 API 很容易 - 我正在寻找一种通过服务帐户访问它的方法。

关于权限部分:

$permissions = array(
    "https://spreadsheets.google.com/feeds",
    "https://docs.google.com/feeds"
);
$credentials = new Google_Auth_AssertionCredentials(
    $googleApiServiceAccount,
    $permissions,
    file_get_contents($googleApiKeyFile)
);

并且 Google API 控制台服务帐户需要作为协作者添加到您要访问的电子表格中!你可以 create/see 此处的服务帐户电子邮件地址:

https://console.developers.google.com/project/YOUR-PROJECT-NAME/permissions/projectpermissions

这是要使用的服务:

$service = new \Google\Spreadsheet\SpreadsheetService();