1
votes

I am working on writing a Play Framework application that interfaces with Azure Active Directory. I'm starting with simply pulling some events but I can't get past the initial token refresh request.

private static void getEventsFromOffice365(){
    System.out.println("getting from O365");

    Promise<String> promise = WS.url("https://login.microsoftonline.com/common/oauth2/token")
    .setBody("grant_type=refresh_token&refresh_token=[refresh token]&scope=openid+offline_access+https%3A%2F%2Foutlook.office.com%2Fmail.read+https%3A%2F%2Foutlook.office.com%2Fcalendars.read+https%3A%2F%2Foutlook.office.com%2Fcontacts.read&redirect_uri=https%3A%2F%2Foauthplay.azurewebsites.net%2F&client_id=[client id]&client_secret=[client secret]")
    .setContentType("application/x-www-form-urlencoded")
    .post("")
    .map(
            new Function<WSResponse, String>() {
                public String apply(WSResponse response) {
                    System.out.println("Done");
                    String result = response.getBody();
                    System.out.println("Result:" + result);
                    System.out.println("json:" + response.getStatus());

                    return result;
                }
            });
}

For some reason whenever I run this I get the following response from Microsoft.

{"error":"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: 6a3c1620-6f4d-4c53-a077-cf1f842c0332\r\nCorrelation ID: 0caba711-d434-4ce9-b15e-a56e27ea5a0f\r\nTimestamp: 2015-11-02 23:31:17Z","error_codes":[90014],"timestamp":"2015-11-02 23:31:17Z","trace_id":"6a3c1620-6f4d-4c53-a077-cf1f842c0332","correlation_id":"0caba711-d434-4ce9-b15e-a56e27ea5a0f"}

As you can see I have the grant_type declared in the post body. Why am I getting this error and how can I solve it?

1
I think there were two mistakes in the code. 1. Using incorrect POST parameters for Endpoint login.microsoftonline.com/common/oauth2/token 2. Be unfamilar with the APIs of Play Framework in Java. Because got the reason response contains the error information ` The request body must contain the following parameter: 'grant_type'` when using the function setBody to set grant_type. For Play Framework's APIs JavaWS & OAuth, please refer to playframework.com/documentation/2.4.x/JavaWS and playframework.com/documentation/2.4.x/JavaOAuth. - Peter Pan

1 Answers

0
votes

From the official documents,you could use the setQueryParameter to pass your parameters. I suggest you can refer to code as following :

WSRequestHolder req=WS.url("https://login.microsoftonline.com/common/oauth2/token");

  req.setQueryParameter("grant_type", refresh_token );
  req.setQueryParameter("refresh_token ", refresh_token);
  req.setQueryParameter("redirect_uri",REDIRECT_URI);
  req.setQueryParameter("scope", scope);
  req.setQueryParameter("client_secret ", client_secret);
  req.setQueryParameter("client_id ", client_id);
  req.setContentType("application/x-www-form-urlencoded")
  req.map(
            new Function<WSResponse, String>() {
                public String apply(WSResponse response) {
                    System.out.println("Done");
                    String result = response.getBody();
                    System.out.println("Result:" + result);
                    System.out.println("json:" + response.getStatus());

                    return result;
                }
            });

At the same time, I suggest you can try to use execute method instead of post method like this :

. execute(“post”)

Any results, please let me know.