为远程计算机执行Google App脚本

时间:2018-08-28 11:56:53

标签: python google-oauth google-api-python-client google-apps-script-api

我正在根据here给定的文档来编写脚本。我的想法是触发一个Google应用程序脚本,该脚本会在Google电子表格中进行一些更新。

我拥有触发应用程序脚本所需的所有详细信息(下面列出)。

  1. 范围
  2. Credentials.json(包含client_id,client_secret,project_id和其他必需的详细信息)
  3. 脚本ID
  4. 提供了适当范围的OAuth令牌和
  5. 将脚本项目部署为API可执行文件

我的Python脚本:

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()

执行此脚本时,我的查询是

  • 它将再次将我重定向到“登录”页面,而不是对其进行授权(附加快照)。
  • 每当我触发脚本时,它就会启动浏览器。这里的想法是在不打开浏览器的情况下运行它。

enter image description here

如何确保即使从远程计算机调用此脚本也能正常工作(这里的想法是远程触发它,并在组织中的任何用户打开它时打开它),并使它成为浏览器上较少事务的事务。 请澄清。

0 个答案:

没有答案