java - Play 框架和 Office 365 OAuth

标签 java azure oauth playframework

我正在编写一个与 Azure Active Directory 交互的 Play Framework 应用程序。我首先简单地提取一些事件,但无法通过初始 token 刷新请求。

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: 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"}

如您所见,我在帖子正文中声明了 grant_type。为什么我会收到此错误以及如何解决它?

最佳答案

来自官方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;
                }
            });

同时,我建议您可以尝试使用 execute 方法而不是 post 方法,如下所示:

. execute(“post”)

有任何结果请告诉我。

关于java - Play 框架和 Office 365 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489018/

相关文章:

java - 循环不工作

Azure应用服务架构理解: Per-App Scaling

ios - 如何使用 ShareKit 和 OAuth 设置 iOS

java - 如何通过双击启动 cli Jar?

java - 有什么办法可以获取listView中的特定 View 吗?

azure - Azure "group"可以用作 Azure Sql 数据库 "Active Directory Admin"吗?

asp.net - 如何将 gulp 与新的 mvc6 标签助手一起用于不同的环境?

oauth - 为什么我们必须在 OAuth 中使用 "change temporary credentials for token credentials"?

javascript - 使用 Jquery 的 Twitter 登录 Oauth 问题

java - 无法构造泛型类型参数,该怎么办?