0

I'm working on a script based on the documentation given here. My idea here is to trigger a google app script which does some updating work in the google spreadsheets.

I've all the details (listed below) required to trigger the app script.

  1. Scopes
  2. Credentials.json (contains client_id, client_secret, project_id and other required details)
  3. Script ID
  4. Provided a properly scoped OAuth token and
  5. Deployed the script project as an API executable

My Python Script:

from __future__ import print_function
from apiclient import errors
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

def main():
    """Runs the sample.
    """
    SCRIPT_ID = '1v3598375_skjghksfjg79_97365XXXXS'

    # Setup the Apps Script API
    # SCOPES = 'https://www.googleapis.com/auth/script.projects'
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    store = oauth_file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets(r'C:\Users\Test\Desktop\credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('script', 'v1', http=creds.authorize(Http()))

    # Create an execution request object.
    request = {"function": "doGet"}

    try:
        # Make the API request.
        response = service.scripts().run(body=request,
                scriptId=SCRIPT_ID).execute()

        if 'error' in response:
            # The API executed, but the script returned an error.
            # Extract the first (and only) set of error details. The values of
            # this object are the script's 'errorMessage' and 'errorType', and
            # an list of stack trace elements.
            error = response['error']['details'][0]
            print("Script error message: {0}".format(error['errorMessage']))

            if 'scriptStackTraceElements' in error:
                # There may not be a stacktrace if the script didn't start
                # executing.
                print("Script error stacktrace:")
                for trace in error['scriptStackTraceElements']:
                    print("\t{0}: {1}".format(trace['function'],
                        trace['lineNumber']))
        else:
            # The structure of the result depends upon what the Apps Script
            # function returns. Here, the function returns an Apps Script Object
            # with String keys and values, and so the result is treated as a
            # Python dictionary (folderSet).
            print ("DONE")

    except errors.HttpError as e:
        # The API encountered a problem before the script started executing.
        print(e.content)


if __name__ == '__main__':
    main()

My Queries here are, when i execute this script,

  • It is redirecting me to the SignIn page again rather than authorizing it (attached snapshot).
  • It is launching a browser whenever i trigger the script. Idea here is to run it without opening the browser.

enter image description here

How can I make sure this script works even when called from the remote machine (the idea here is to trigger it remotely and open when any user within the organization opens it) and also make it a browser less transaction. Kindly clarify.

sdgd
  • 723
  • 1
  • 17
  • 38
  • Sounds like you might want a service account instead of to act as the user requesting. Also note that your code will run the flow even if the credentials are simply expired and need to be refreshed. The client library will happily refresh them if possible when making a request. – tehhowch Aug 28 '18 at 12:20
  • yeah but when i run this script from a remote machine, it's redirecting me to the SignIn page (updated in the question) – sdgd Aug 28 '18 at 12:27
  • I agree that this probably requires to use a service account to work with browser-less authentication, but per the linked documentation, it does not appear that service accounts can be used. – bstovall Apr 10 '21 at 05:44

0 Answers0