I'd like to use a service account to access a Google Sheet via the Apps Script Execution API, but it's not clear from the documentation whether this is supported.
The steps I've tried (which result in a 403 status from the Execution API) are:
- Create a new (unbound) Apps Script
- Visit the linked Developer Console project
- Enable the Execution API
- Create a new service account within the same project (downloading the generated JSON file)
- Create a new Google Sheet and share it with the service account's email address (this is the step I'm least sure about)
- Write an apps script function that reads from the spreadsheet
- Run the script manually from the Script Editor (to set the scopes on the script correctly)
- Publish the script ("Deploy as API executable"), making it accessible to 'anyone'
- Mint a new OAuth2 token using the service account and the scopes linked to the script (in our case just 'https://www.googleapis.com/auth/spreadsheets')
- Attempt to make a call to the Execution API using the token
This is the response I got:
{
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}
}
Does this not work because Service Accounts are never able to access the Execution API? Or is there something wrong with the steps above?