Home > Blockchain >  401 unauthorized : [no body] with root cause in JAVA Spring
401 unauthorized : [no body] with root cause in JAVA Spring

Time:12-15

I'm creating my first REST API using JAVA Spring and when I'm making a rest call to an external API, I get

401 Unauthorized: [no body]

I think my problem is here:

requestParams.add("Grant_type", "client_credentials");

I saw some questions related to this but none well able to solve my problem.

Spring REST template - 401 Unauthorized error

Spring Boot Callable - 401 Unauthorized: [no body]

JAVA code:

public String getAuth(String client_id, String app_secret) {
        String auth = client_id   ":"   app_secret;
        return Base64.getEncoder().encodeToString(auth.getBytes());
}

@GetMapping(value = "/token")
public Object generateAccessToken() {
      String auth = this.getAuth(
            "CLIENT_ID",
            "APP_SECRET"
      );
    
      RestTemplate restTemplate = new RestTemplate();
    
      String base = "https://external-api.com";
      HttpHeaders headers = new HttpHeaders();
      headers.set("Authorization", "Basic "   auth);
    
      MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<>();
            requestParams.add("Grant_type", "client_credentials");
          
      ResponseEntity<Object> response = restTemplate.postForEntity(
              base   "/v1/oauth2/token",
              requestParams,
              Object.class,
              headers
       );        

       return response.getBody();
}

CodePudding user response:

Here's the solution to my own question.

This is what I had to change;

MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
            requestBody.add("grant_type", "client_credentials");
    
            HttpEntity<?> request = new HttpEntity<>(requestBody, headers);
    
            ResponseEntity<String> response = restTemplate.postForEntity(
                    base  "/v1/oauth2/token",
                    request,
                    String.class
            );

Here's the final solution:

public String generateAccessToken() {
String base = "example-api.com";
        String auth = this.getAuth(
                "client id",
                "app_id"
        );

        // create an instance of RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // create headers
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("Authorization", "Basic "   auth);

        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("grant_type", "client_credentials");

        HttpEntity<?> request = new HttpEntity<>(requestBody, headers);

        ResponseEntity<String> response = restTemplate.postForEntity(
                base  "/v1/oauth2/token",
                request,
                String.class
        );

        // check response
        if (response.getStatusCode() == HttpStatus.OK) {
            System.out.println("Request Successful");
            System.out.println(response.getBody());
        } else {
            System.out.println("Request Failed");
            System.out.println(response.getStatusCode());
        }

        JSONObject object = new JSONObject(response.getBody());
        return object.getString("access_token");
    }
  • Related