2015-11-02 52 views
1

我正在編寫一個與Azure Active Directory連接的Play Framework應用程序。我只是簡單地拉一些事件,但我無法超越最初的令牌刷新請求。Play Framework和Office 365 OAuth

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; 
       } 
      }); 
} 

出於某種原因,每當我運行此我從微軟得到下面的響應。

{ 「錯誤」: 「INVALID_REQUEST」, 「ERROR_DESCRIPTION」:「AADSTS90014:請求體必須包含以下參數: 'grant_type' \ r \ nTrace ID:6a3c1620-6f4d-4C53-A077-cf1f842c0332 \ r \ n相關ID:0caba711-d434-4ce9-b15e-a56e27ea5a0f \ r \ n時間戳: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」}

正如你可以看到我在帖子正文中聲明瞭grant_type。爲什麼我得到這個錯誤,我該如何解決它?

+0

我認爲在代碼中有兩個錯誤。 1.對端點使用不正確的POST參數https://login.microsoftonline.com/common/oauth2/token 2.與Java中Play Framework的API不相似。因爲得到原因響應包含錯誤信息'當使用函數'setBody'設置'grant_type'時,請求體必須包含以下參數:'grant_type'。 對於Play Framework的API JavaWS和OAuth請參考https://www.playframework.com/documentation/2.4.x/JavaWS和https://www.playframework.com/documentation/2.4.x/ JavaOAuth。 –

回答

0

從官方documents,您可以使用setQueryParameter 來傳遞你的參數。我建議你可以參考代碼如下:

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; 
       } 
      }); 

同時,我建議你可以嘗試使用,而不是post方法是這樣execute方法:

. execute(「post」) 

任何結果,請讓我知道。

+0

不幸的是,這仍然給了我相同的結果。我必須從執行方法調用map,像 'req.execute(「post」)。map(...)' –