Missing required parameter 'grant_type'

Vijay_Kumar
Vijay_Kumar Newcomer
edited September 9 in CFS Bulk File/TM3

I have set the machine id, password, appkey and all required parameters in the HTTP request message to the /auth/oauth2/v1/token endpoint via the URI query parameters. However, I got missing required parameter error returns from the API.

{"error":{"id":"xxx","code":"400","message":"Validation error","status":"Bad Request","errors":[{"key":"grant_type","reason":"Missing required parameter 'grant_type'"}]}}

Tagged:

Answers

  • Hello @Vijay_Kumar

    Thank you for reaching out. The error "Missing required parameter 'grant_type'" indicates there is something wrong with the request message that makes the RDP think your message missing some parameters.

    Could you please share the code that create a request message and send over HTTP?

  • Vijay_Kumar
    Vijay_Kumar Newcomer
    edited September 9

    Hello @wasin_w2

    Here the code

    HttpClient httpAuth=null;
    HttpRequest reqMsg=null;
    HttpResponse<String> respMsg=null;
    URI authUri=null;

    httpAuth = HttpClient.newBuilder().proxy(ProxySelector.of(new InetSocketAddress(proxyHost, proxyPort))).build();
    lsegAuthUri=new URIBuilder(authTokenUrl)
    .setParameter("username", userAccount)
    .setParameter("password", password)
    .setParameter("grant_type", "password")
    .setParameter("scope", "trapi")
    .setParameter("client_id", "app-key")
    .build();

    reqMsg=HttpRequest.newBuilder()
    .version(HttpClient.Version.HTTP_2)
    .header("Content-Type", "application/x-www-form-urlencoded")
    .timeout(Duration.ofSeconds(300))
    .uri(authUri)
             .POST(BodyPublishers.ofString(""))
             .build();

    respMsg=httpAuth.send(reqMsg, HttpResponse.BodyHandlers.ofString());
    System.out.println(respMsg.statusCode());
    System.out.println(respMsg.body().toString());

  • wasin.w V3
    wasin.w V3 admin
    edited September 9

    Hello @Vijay_Kumar

    Thank you for the code.

    I have reviewed the code.  The HTTP Post request needs the authentication parameters via the body payload. However, the code uses Apache HTTPClient’s URIBuilder.setParameter() method to set the authentication parameters via the URI query string. That is why the RDP API returns HTTP status 400 with "Missing required parameter 'grant_type'" (because it cannot find a required parameter on the request message payload).

    The example code in Java should be like this.

    HttpClient httpAuth=null;
    HttpRequest reqMsg=null;
    HttpResponse<String> respMsg=null;
    URI authUri=null;

    Map<String, String> parameters = new HashMap<>();
    parameters.put("client_id", "app-key");
    parameters.put("username", username);
    parameters.put("password", password);
    parameters.put("grant_type", "password");
    parameters.put("scope", "trapi");
    parameters.put("takeExclusiveSignOnControl", "true");

    String form = parameters.entrySet()
    .stream()
    .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
    .collect(Collectors.joining("&"));

    httpAuth = HttpClient.newBuilder().build();

    authUri=new URIBuilder(authTokenUrl).build();

    reqMsg=HttpRequest.newBuilder()
    .version(HttpClient.Version.HTTP_2)
    .header("Content-Type", "application/x-www-form-urlencoded")
    .timeout(Duration.ofSeconds(300))
    .uri(authUri)
         .POST(HttpRequest.BodyPublishers.ofString(form))
         .build();

    respMsg=httpAuth.send(reqMsg, HttpResponse.BodyHandlers.ofString());