0

Requirement is to send notification with auth token. And to get token we have been provided below curl command. I need to implement curl equivalent code to get the token so that I can use that token while sending the notification. I have implemented but it is not working whereas curl command is working fine. Please check below curl command and implemented code.

**CURL Command **

curl --location --request GET 'https://test.appleexpress.com/orderservicesapi/token' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Cookie: visid_incap_2670687=PgVnXOJwTveM+f+unLbHivuZr2QAAAAAQUIPAAAAAAAGR2Wbz5NgMv8yRX/u7VZP' --data-urlencode 'username=3062:HONGKONGWISE' --data-urlencode 'password=mjtwj2v7YfsUstu' --data-urlencode 'grant_type=password'

**Implemented Code **

public void getToken() throws UnsupportedEncodingException {

        String username = "3062:HONGKONGWISE";
        String password = "mjtwj2v7YfsUstu";
        String grantType = "password";
        String params = "username=" + URLEncoder.encode(username, "UTF-8") +
                "&password=" + URLEncoder.encode(password, "UTF-8") +
                "&grant_type=" + URLEncoder.encode(grantType, "UTF-8");

        ResponseEntity<String> responseEntity = null;
        RestTemplate restTemplate = utils.getClientTimeOutInRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("Cookie", "visid_incap_2670687=PgVnXOJwTveM+f+unLbHivuZr2QAAAAAQUIPAAAAAAAGR2Wbz5NgMv8yRX/u7VZP");
        HttpEntity<String> entity = new HttpEntity<>(params,headers);


        try {
            log.info("RMS_SERVER : getToken : Authorization API : {}", authUrl);
            log.info("RMS_SERVER : getToken : Sendind Request to authorization API : {}", entity);

            responseEntity = restTemplate.exchange(authUrl, HttpMethod.GET, entity, String.class);

            if (responseEntity.getStatusCode().is2xxSuccessful() && null != responseEntity.getBody()) {
                log.info("RMS_SERVER : getToken : Response of authorization API is: {}", responseEntity.getBody());
                String authRequest = responseEntity.getBody();
                JSONObject jsonAuthRequest = new JSONObject(authRequest);
                this.setAuthtoken(jsonAuthRequest.getString(ACCESS_TOKEN));
                log.info("RMS_SERVER : getToken : Token successfully received ");
            } else {
                log.error("RMS_SERVER : getToken : Token not received ");
            }
        } catch (HttpStatusCodeException e) {
            log.error(String.format("RMS_SERVER : getToken : HttpStatusCodeException Occurred Caused By: %s : Status Code : %s ", e.getResponseBodyAsString(), e.getStatusCode()));
            throw new AppException(e.getResponseBodyAsString(), e.getStatusCode());
        } catch (ResourceAccessException e) {
            log.error(String.format("RMS_SERVER : getToken : ResourceAccessException Occurred Caused By: %s", e.getLocalizedMessage()));
            throw new AppException(e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (RestClientException e) {
            log.error("RMS_SERVER : getToken : Received exception while sending Request to authorization API: %s" + e.getLocalizedMessage());
            throw new AppException(e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

I tried to implement the code but not succeeded getting below error. Auth API:- https://test.appleexpress.com/orderservicesapi/token

2023-07-27T07:20:16.640Z  INFO  26520 --- [scheduling-1] com.gor.si.service.impl.AuthServiceImpl: RMS_SERVER : getToken : Authorization API : https://test.appleexpress.com/orderservicesapi/token
2023-07-27T07:20:16.640Z  INFO  26520 --- [scheduling-1] com.gor.si.service.impl.AuthServiceImpl: RMS_SERVER : getToken : Sendind Request to authorization API : <username=3062%3AHONGKONGWISE&password=mjtwj2v7YfsUstu&grant_type=password,[Content-Type:"application/x-www-form-urlencoded", Cookie:"visid_incap_2670687=PgVnXOJwTveM+f+unLbHivuZr2QAAAAAQUIPAAAAAAAGR2Wbz5NgMv8yRX/u7VZP"]>
2023-07-27T07:20:17.799Z  ERROR 26520 --- [scheduling-1] com.gor.si.service.impl.AuthServiceImpl: RMS_SERVER : getToken : HttpStatusCodeException Occurred Caused By: {"error":"unsupported_grant_type"} : Status Code : 400 BAD_REQUEST 
2023-07-27T07:20:17.803Z  ERROR 26520 --- [scheduling-1] org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler: Unexpected error occurred in scheduled task
com.gor.si.exception.AppException: {"error":"unsupported_grant_type"}
    at com.gor.si.service.impl.AuthServiceImpl.getToken(AuthServiceImpl.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
amar
  • 1
  • 1
  • You are sending a body not parameters. Use a `MultiValueMap` to store the parameters and send that as a body. – M. Deinum Jul 27 '23 at 07:44
  • Still not working getting same error. MultiValueMap map = new LinkedMultiValueMap<>(); map.add("username", username); map.add("password", password); map.add("grant_type", grantType); HttpEntity> entity = new HttpEntity<>(map,headers); – amar Jul 27 '23 at 08:12
  • Please don't add code as comments as that is totally unreadable, also dont urlencode the values as that will be automatically done/ – M. Deinum Jul 27 '23 at 08:31
  • removed urlencode but still not working. – amar Jul 27 '23 at 08:47

0 Answers0