Chamada de API com o serviço DatasetSP.save na entidade "ApontamentoPA"

Opa, pessoal! Estou tendo dificuldade ao fazer uma requisição para o serviço DatasetSP.save na entidade ApontamentoPA. O response da requisição retorna sempre status 3 :

2023-07-26 10:30:37,352 INFO  [stdout] (default task-3143) método utilitarios.Utils.post() - response: {"serviceName":"DatasetSP.save","status":"3","pendingPrinting":"false","transactionId":"0EEC4C4FBD413D065F76B428614E0628","statusMessage":"Não autorizado."}

Se eu chamar o serviço pelo Insomnia, a requisição executa tranquilamente. Ao fazer por dentro do Sankhya, retorna o erro citado. Segue código da requisição:


public static JSONObject post(String endPoint, String json) throws MGEModelException {
        System.out.println("método utilitarios.Utils.post() - Início \n endPoint: " + endPoint + " json: " + json);
        boolean isSuccess = false;
        String session = ServiceContext.getCurrent().getHttpSessionId();
        String url = ServiceContext.getCurrent().getHttpRequest().getLocalAddr();
        String porta = String.valueOf(ServiceContext.getCurrent().getHttpRequest().getLocalPort());
        String protocol = ServiceContext.getCurrent().getHttpRequest().getProtocol().split("/")[0].toLowerCase(Locale.ROOT);
        String localHost = protocol + "://" + url + ":" + porta;

        HttpServletRequest servletRequest = ServiceContext.getCurrent().getHttpRequest();
        Cookie cookie = findCookieByName(servletRequest, "JSESSIONID");

        System.out.println("Cookie: " + cookie != null ? cookie.getValue() : null);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.connectTimeout(2, TimeUnit.MINUTES);
        builder.writeTimeout(2, TimeUnit.MINUTES);
        builder.readTimeout(2, TimeUnit.MINUTES);

        OkHttpClient client = builder.build();

        MediaType mediaType = MediaType.parse("application/json");

        RequestBody requestBody = RequestBody.create(mediaType, json);
        System.out.println("URL:" + localHost + "/" + endPoint + "&outputType=json&mgeSession=" + session);
        Request request = new Request.Builder()
               .url(localHost + "/" + endPoint + "&outputType=json&mgeSession=" + session+"&jsessionid="+session)
                //.url(localHost + "/" + endPoint + "&outputType=json&mgeSession=8u8wNV72w2Sw0D_zxICtJyNMrmTsUdvNdbtII3lz")
                .post(requestBody)
                .addHeader("cookie", "JSSESSION=" + (cookie != null ? cookie.getValue() : null))

                //.addHeader("cookie", "JSSESSION=8u8wNV72w2Sw0D_zxICtJyNMrmTsUdvNdbtII3lz.sankhya-w-5854ff6664-2gwpv")
                .addHeader("Content-Type", "application/json")
                .build();

        System.out.println("método utilitarios.Utils.post() - Antes de executar a requisição");
        try (Response response = client.newCall(request).execute()) {

            if (!response.isSuccessful()) {
                mostraErro("Não foi possível fazer a requisição: \n" + json);
            }


            String retorno = response.body() != null ? response.body().string() : "";

            System.out.println("método utilitarios.Utils.post() - response: " + retorno);

            JSONObject jsonResponse = new JSONObject(retorno);

            String codigo = jsonResponse.has("status") ?
                    jsonResponse.getString("status") : "Z9";

            String statusMessage = jsonResponse.has("statusMessage") ?
                    jsonResponse.getString("statusMessage") : "Z9";


            if ("0".equals(codigo)) {
                mostraErro(statusMessage);
            } else if ("Z9".equals(codigo)) {
                mostraErro("Não foi possível identificar o erro. Contate o administrador");
            } else {
                isSuccess = true;
            }

            return jsonResponse;

        } catch (IOException e) {
            e.printStackTrace();
            mostraErro("Ocorreu um erro na requisição: \n" + e.getMessage());
            return new JSONObject("{\"erro\":\"Erro\"}");
        } finally {

        }


    }


Esta é um exemplo da chamada do método post:


String json = "{\"serviceName\":\"DatasetSP.save\",\"requestBody\":{\"entityName\":\"ApontamentoPA\",\"fields\":[\"NUAPO\",\"SEQAPA\",\"QTDAPONTADA\",\"QTDPERDA\",\"CODPRODPA\"],\"records\":[{\"pk\":{\"NUAPO\":\""+nuApo+"\",\"SEQAPA\":\"1\"},\"foreignKey\":{\"NUAPO\":\""+nuApo+"\"},\"values\":{\"2\":\""+qtdApontada+"\",\"3\":\""+qtdPerda+"\"}}]}}\n";
       Utils.post("mge/service.sbr?serviceName=DatasetSP.save",json);

3 respostas