【发布时间】:2015-11-02 23:40:21
【问题描述】:
我正在编写一个与 Azure Active Directory 交互的 Play Framework 应用程序。我开始只是简单地提取一些事件,但我无法通过初始令牌刷新请求。
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;
}
});
}
出于某种原因,每当我运行此程序时,我都会收到来自 Microsoft 的以下响应。
{"error":"invalid_request","error_description":"AADSTS90014:请求正文必须包含以下参数:'grant_type'。\r\n跟踪 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。为什么会出现此错误,我该如何解决?
【问题讨论】:
-
我认为代码中有两个错误。 1. Endpoint login.microsoftonline.com/common/oauth2/token 使用错误的 POST 参数 2. 不熟悉 Java 中 Play Framework 的 API。因为得到原因响应包含错误信息`请求正文必须包含以下参数:'grant_type'`时使用函数
setBody设置grant_type。 Play Framework的APIJavaWS&OAuth,请参考playframework.com/documentation/2.4.x/JavaWS和playframework.com/documentation/2.4.x/JavaOAuth。
标签: java azure oauth playframework