“未找到所请求的实体." -Apps脚本执行API错误 [英] "Requested entity was not found." - Apps Script Execution API error

查看:78
本文介绍了“未找到所请求的实体." -Apps脚本执行API错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an Apps Script that is installed in five G Suite accounts. I am invoking the app scripts from the Java code that is being deployed in Google App Engine. I have stored the five refresh tokens in a properties file and set them in GoogleCredential in a round robin fashion before invoking the Apps Script. When I am trying to invoke the Apps Script Requested entity was not found., an error is returned. But the same refresh tokens and client secrets work fine when I create a simple java program to invoke the Apps Script.

@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);
    }

}

推荐答案

问题不是与Apps Script或App Engine项目的配置有关.我在@PostConstruct方法中创建了GoogleCredential对象,并在调用Apps脚本之前从预生成的令牌列表中设置了刷新令牌(credential.setRefreshToken()).即使我为同一组Client ID和Client Secret创建了刷新令牌,也遇到了错误.因此,我将创建多个GoogleCredential对象作为刷新令牌的数量,而不是在多个刷新令牌之间重用凭据对象.

The issue was not with the configuration of Apps Script or the App Engine project. I was creating a GoogleCredential object in the @PostConstruct method and set the refresh tokens (credential.setRefreshToken()) from the list of pre-generated tokens before invoking the Apps Script. Even though I had created the refresh tokens for the same set of Client ID and Client Secret, I was getting the error. So, I am creating multiple GoogleCredential objects as the number of refresh tokens and not re-use the credentials object across multiple refresh tokens.

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);
}

这篇关于“未找到所请求的实体." -Apps脚本执行API错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆