1

I am getting exception while generating an Access Token using feign client. The same payload is working fine in the Postman.

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("grant_type", "client_credentials");
map.add("client_id", "67881e5b-f5d5-4085-8762-c35b7b6aeede");
map.add("client_secret", "D-85Pg3wN63dmznxa-puB_89Po~o5CsKhA");
map.add("scope", "https://graph.microsoft.com/.default");

AccessTokenResponse openIdTokenResponse = graphAPILoginFeignClient.getAccessIdToken("5494cc2e-fb14-4a2d-bb5e-bf164d9141cf",request);

Feignclient code:

@FeignClient(name = "GraphAPILoginFeignClient", url = "${graphApiLoginUrl}")
public interface GraphAPILoginFeignClient {
   @PostMapping(value = "/{tenantID}/oauth2/v2.0/token",consumes = MediaType.APPLICATION_JSON_VALUE)
   AccessTokenResponse getAccessIdToken(@PathVariable("tenantID") String tenantID,
                                        @RequestBody MultiValueMap<String, Object>  request);
}

Exception:

{
  "timestamp": "2021-01-27T17:30:34.456+00:00",
  "message": "[400 Bad Request] during [POST] to [https://login.microsoftonline.com/5494cc2e-fb14-4a2d-bb5e-bf164d9141cf/oauth2/v2.0/token]    [GraphAPILoginFeignClient#getAccessIdToken(String,AuthorizationTokenRequest)]: 
  [{\"error\":\"invalid_request\",
    \"error_description\":\"AADSTS900144: The request body must contain the 
                                          following parameter: 'grant_type'.\\r\\n
    Trace ID: b8ef5f37-95f7-4427-8f0e-146a34b65000\\r\\n
    Correlation ID: ... (503 bytes)]","details": "uri=/accessmanagement/allusers"
}

Same request payload working from Postman: https://i.stack.imgur.com/zxFVa.png

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63

1 Answers1

0

I had the same problem and the same code.

I was able to successfully execute the code by replacing the class method MultiValueMap::put with MultiValueMap::set.

In the LinkedMultiValueMap class, these methods are implemented differently.

Nazar Usik
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31852446) – Sumit Sharma May 28 '22 at 17:33