How to access Google spreadsheets with service account credentials?

I created a server side application in PHP that should work with Google Spreadsheets.

I can successfully authenticate with OAuth 2.0 authentication, but when I request a list of spreadsheets from Google, I get only spreadsheets shared with the service account by the owner of the spreadsheet.

Is there a way that a service account could retrieve all the spreadsheets that belong to my main account and not the service, including those that are not explicitly shared with the service account?

Also, I still want the spreadsheets to be private, so no one can access them without my permission, but I need a service account to have full access to existing and new tables.

Any advice is appreciated.

+4
source share
3 answers

You must change the approach:

  • Create a service account in Drive. You can only manage and use this account using the API (and not with the web interface, as usual)

  • API- Drive , , , . , - API - , , .

, : https://developers.google.com/drive/v2/reference/permissions

+1

script, , Google Spreadsheet. README:

https://github.com/juampynr/google-spreadsheet-reader

+2

, Google Apps, GMail. .

, , Google: ( p12) . Google Apps, . .

:

  • Google Apps
  • , API

API API- .

, . . - .

, , :

$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
      'YOUR_SERVICE_ACCOUNT_EMAIL',
      array('https://spreadsheets.google.com/feeds'),
      $key);
$auth->sub = 'yourmainaccount@domain.com';

$auth OAuth, API . , , , , .

Also note that this token will expire after 1 hour, and then the API will start returning errors Session Expired. If your client does not handle this automatically, you need to catch the error and restore the token.

0
source

All Articles