Reputation: 5
I am running the Google App script executable API from a Python script. I am doing this using a service account so that it can be scheduled for unattended run.
I have tried to do this using my own account and OAuth works by opening up a browser page and asking for authorization. After I authorize the script is executed. I have authorized the scopes to the service account as suggested in the below link
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
from __future__ import print_function
from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
from google.oauth2 import service_account
import googleapiclient.discovery
import httplib2
import json
SCRIPT_ID ='xyz'
SCOPES = ['https://www.googleapis.com/auth/documents' ,'https://www.googleapis.com/auth/forms', 'https://www.googleapis.com/auth/presentations','https://www.googleapis.com/auth/spreadsheets']
service_account_info = json.load(open('service_account_key_file.json'))
creds = service_account.Credentials.from_service_account_info( service_account_info)
service = build('script', 'v1', credentials=creds)
request = {"function": "testExecutableSA","parameters": ["TEST RUN"]}
try:
response = service.scripts().run(body=request,
scriptId=SCRIPT_ID).execute()
currentdata = response
print(currentdata)
except errors.HttpError as e:
# The API encountered a problem before the script started executing.
print(e.content)response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute()
When I do it using my account the script runs and a test message is logged via the App script that is being called "This is a "Test Run"
When I do it using the service account file I get the below error
b'{\n "error": {\n "code": 400,\n "message": "Request contains an invalid argument.",\n "errors": [\n {\n "message": "Request contains an invalid argument.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
There is no detail on what is the invalid argument.
Only this that changes is the http_authorized
object that is passed.
Am I making a mistake or something is broken ?
Upvotes: 0
Views: 1011
Reputation: 8964
According to the official documentation the Apps Script API does not work with service accounts.
Upvotes: 1