0

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 ?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • If you are required to run the Google Apps Script using the Service Account, how about this workaround? In this workaround, the Google Apps Script is run using Web Apps. By using Web Apps, you can run the Google Apps Script using the access token retrieved from the Service Account. [Ref](https://github.com/tanaikech/taking-advantage-of-Web-Apps-with-google-apps-script) But I'm not sure whether this is the direction you want. So I posted a simple information as a comment. – Tanaike May 11 '19 at 01:54
  • Appreciate your help. I think this will be useful and worth trying out for me. My sole intention is to run the App scripts from a backed Python but unattended. If the method you described can help I will be very happy. I will give it a try and tell you how it works !!! thanks a lot – umesh shetty May 11 '19 at 04:52
  • Thank you for replying. I would like to wait for your result. – Tanaike May 11 '19 at 06:39
  • Hi, I tried to use this method bu the issue I have is the app script can be deployed as a web app and I can allow permission to anyone with my domain xyz.com. But when I run this script from Python , that python app is not in the google domain and hence I get unauthorized access. Can you suggest how do I get the token for accessing it form a third party app ? – umesh shetty May 16 '19 at 06:01
  • Thank you for replying. The new issue of your comment is different from your this question. So can you post the new issue as new question? At that time, please include the detail information of your new issue. By this, a lot of users can see your issue and think of your solution. If you can cooperate to resolve your issue, I'm glad. – Tanaike May 16 '19 at 06:16
  • Hi Tanaike. Did you found the solution or any workaround I am stuck with the same issue. – Abhimanyu May 15 '20 at 14:09

1 Answers1

1

According to the official documentation the Apps Script API does not work with service accounts.

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30