2

我们有一个安装在五个 G Suite 帐户中的 Apps 脚本。我正在从部署在 Google App Engine 中的 Java 代码调用应用程序脚本。我已将五个刷新令牌存储在属性文件中,并在调用 Apps 脚本之前以循环方式将它们设置在 GoogleCredential 中。当我尝试调用 Apps ScriptRequested entity was not found.时,会返回错误。但是,当我创建一个简单的 java 程序来调用 Apps 脚本时,相同的刷新令牌和客户端密码可以正常工作。

@Service
public class GoogleAppsScriptServiceImpl {
    private String[] scriptIds;

    private String[] refreshTokens;

    private GoogleCerdential credential;

    public void executeAppsScript() {
        List<Object> params = new ArrayList<>();
        params.add(googleDocFileId);

        ExecutionRequest request = new ExecutionRequest()
                                        .setFunction(APPS_SCRIPT_FUNCTION_NAME)
                                        .setParameters(params);

        int index = new Random().nextInt(numOfUsers);

        Script scriptService = getScriptService(refreshTokens[index]);
        String scriptId = scriptIds[index];

        Operation operation = scriptService.scripts()
                            .run(scriptId, request)
                            .setQuotaUser(UUID.randomUUID().toString())
                            .execute();
    }

    private Script getScriptService(String refreshToken) {
        credential.setRefreshToken(refreshToken);
        return new Script.Builder(httpTransport, jsonFactory, credential)
                         .setApplicationName(APPLICATION_NAME)
                         .build();
    }

    @PostConstruct
    private void createGoogleCredential() throws Exception {
        jsonFactory = JacksonFactory.getDefaultInstance();
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        credential = new GoogleCredential.Builder()
                                    .setTransport(httpTransport)
                                    .setJsonFactory(jsonFactory)
                                    .setClientSecrets(clientId, clientSecret)
                                    .build();

        refreshTokens = commaDelimitedListToStringArray(refreshTokensProp);
        numOfUsers = refreshTokens.length;

        scriptIds = commaDelimitedListToStringArray(scriptsIdsProp);
    }

}
4

2 回答 2

2

您正在将 Execution API 部署到独立脚本类型或容器绑定脚本类型的项目。然后您使用 API 调用项目中的函数。在这种情况下,Requested entity was not found.就会发生错误。如果我的理解是正确的,我也经历过同样的情况。那么如何确认以下几点呢?

  1. 使用保存按钮,再次保存正在部署 API 可执行文件的项目。
    • 这对我来说非常重要。
  2. 将项目另存为新版本,然后重新部署 API。
  3. 是否从使用 Apps Script API 的项目中检索到客户端 ID 和客户端密码。
    • 是否从这些客户端 ID 和客户端密码中检索访问令牌。
  4. 范围是否包括https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.scriptshttps://www.googleapis.com/auth/script.external_request.
  5. 是否启用执行 API。

如果你想简单地使用 curl 命令进行测试,你也可以使用以下命令。

curl -X POST -L \
    -H "Authorization: Bearer ### access token ###" \
    -H "Content-Type: application/json" \
    -d "{function: '### function name ###',devMode: true}" \
    "https://script.googleapis.com/v1/scripts/### script ID ###:run"

确认以上几点后,请重试。如果这些对你没有用,我很抱歉。或者如果我误解了你的问题,我真的很抱歉。

于 2018-03-10T09:04:27.537 回答
1

问题不在于 Apps 脚本或 App Engine 项目的配置。我在方法中创建了一个GoogleCredential对象,并在调用 Apps 脚本之前从预生成的令牌列表中@PostConstruct设置了刷新令牌 ( )。credential.setRefreshToken()即使我为同一组客户端 ID 和客户端密码创建了刷新令牌,我还是收到了错误消息。因此,我创建了多个GoogleCredential对象作为刷新令牌的数量,而不是跨多个刷新令牌重用凭据对象。

private GoogleCredential[] credentials;

@PostConstruct
private void createGoogleCredential() throws Exception {
    jsonFactory = JacksonFactory.getDefaultInstance();
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    String[] refreshTokens = commaDelimitedListToStringArray(refreshTokensProp);
    numOfUsers = refreshTokens.length;

    credentials = new GoogleCredential[numOfUsers];
    for (int i=0; i<numOfUsers; i++) {
        GoogleCredential credential = new GoogleCredential.Builder()
                                        .setTransport(httpTransport)
                                        .setJsonFactory(jsonFactory)
                                        .setClientSecrets(clientId, clientSecret)
                                        .build();

        credential.setRefreshToken(refreshTokens[i]);

        credentials[i] = credential;
    }

    scriptIds = commaDelimitedListToStringArray(scriptsIdsProp);
}
于 2018-03-12T16:03:37.887 回答