I am successfully using the Google PHP API with an service account to manipulate Google CloudDNS records like this:

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

[..]

Is it possible to use a similar scheme for Google Apps Spreadsheets access? What to use as $permission and what kind of API do i have to enable in the Google Developer Console?

Please note: I am fully aware of the fact that it is easy to access the API via oAuth2 - i am looking for a way to access it via a service account.

1

There are 1 best solutions below

0
On BEST ANSWER

Regarding the permission part:

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

And the Google API Console Service Account needs to be added as collaborator to the spreadsheet you want to access! You can create/see the service account email addresses here:

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

This is the service to use:

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