phplistgoogle-sheetsshared

How to access Google Spreadsheets with a service account credentials?


I have created a server side application in PHP that's supposed to work with Google Spreadsheets.

I'm able to authenticate successfully with OAuth 2.0 authentication, but when requesting the list of the spreadsheets from Google, I only get the spreadsheets shared with the service account by the spreadsheet owner.

Is there a way that service account could retrieve all the spreadsheets owned by my main account not the service one, including those not explicitly shared with the service account?

Also I still want to keep the spreadsheets private so noone can access them without my permission, but I need the service account to have full access to both existing and new spreadsheets.

Any advice is appreciated.


Solution

  • You have to change approach:

    1. Create a service account in Drive. You can manage and use this account only with the API (not with the web interface as usually)

    2. With the Drive API you can list, create, update, delete and change the permissions of the files. When you create a new file, you can share it - always with API - to the users you want, make the new file public or change the ownership.

    Please take a look: https://developers.google.com/drive/v2/reference/permissions