java - 如何使用 Google API 服务库 (Java) 运行 Google App 脚本

标签 java google-apps-script google-api-client

按照 Java Quickstart example 进行操作,我能够创建一个新的 Google App Script 项目并检索 scriptId。另外,通过引用Restful API document ,该脚本应该能够使用方法:scripts.run执行。但是,我不知道如何在 Java 中使用 com.google.api.services.script.Script 检索返回值。

我已经尝试过:

Script scriptService = getScriptService();
Script.Scripts scripts = scriptService.scripts();
Script.Scripts.Run run = scripts.run(scriptId, request);

和反编译的run函数:

public Script.Scripts.Run run(String var1, ExecutionRequest var2) throws IOException {
    Script.Scripts.Run var3 = new Script.Scripts.Run(var1, var2);
    Script.this.initialize(var3);
    return var3;
}

该函数没有返回我正在寻找的 ExecutionResponse 对象。

最佳答案

根据 REST API 文档,调用 script.run不会立即返回 ExecutionResponse 对象,而是返回 Operation可能包含 ExecutionResponse 的对象:

{
  "done": boolean,

  // Union field result can be only one of the following:
  "error": {
    object(Status)
  },
  "response": object(ExecutionResponse)
  ,
  // End of list of possible types for union field result.
}

如果我们看一下Java API Client library ,我们看到该方法 Script.Script.run接受脚本 ID 和 ExecutionRequest 参数,然后返回 Script.Script.Run 请求,该请求必须是 .execute()d:

Create a request for the method "scripts.run". This request holds the parameters needed by the script server. After setting any optional parameters, call the AbstractGoogleClientRequest.execute() method to invoke the remote operation.

引用的文档引用的请求是 Script.Script.Run ,并且具有诸如 .setAccessToken() 之类的方法用于附加配置,以及一些执行方法,例如 .execute().executeMedia()提交执行请求并返回Operation。

关于java - 如何使用 Google API 服务库 (Java) 运行 Google App 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49357289/

相关文章:

javascript - 如何删除 Google 脚本中的 Google 缓存?

google-apps-script - 如何使用 Apps 脚本将日期插入 YYYY-MM-DD 纯文本到 Google 电子表格中

android - GoogleApiClient 无法连接

ios - Google 表格 API 发布 404 错误

c# - 避免 Google Calendar API 在人们的日历中添加实际事件?

java - 从字节创建 3DES key

java - 使用 Axis 1.4 设置自定义 SOAP header

java - Jasper Report—— HashMap 或子报告

java - Shiro 'Any' 权限实例

javascript - 尝试将数据从 Google 电子表格发布到外部 API