有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在尝试从Azure AD获取访问令牌时获取响应代码400

我正在为我的web应用程序实现azure,并尝试通过以下openId connect教程获取访问令牌

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code

当我请求获取访问令牌时,我总是收到错误的请求

请求获取访问令牌:

POST /{tenant}/oauth2/token HTTP/1.1

Host: https://login.microsoftonline.com

Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code

&client_id=2d4d11a2-f814-46a7-890a-274a72a7309e

&code=AwABAAAAvPM1KaPl.......

&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F

&resource=https%3A%2F%2Fservice.contoso.com%2F

&client_secret=p@ssw0rd

这是我的代码:

public static String post( String endpoint,
        Map<String, String> params) {//YD
    StringBuffer paramString = new StringBuffer("");
    //if(!Utilities.checkInternetConnection(context)){
    //  return XMLHandler.getXMLForErrorCode(context, JSONHandler.ERROR_CODE_INTERNET_CONNECTION);
    //}
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    StringBuffer tempBuffer = new StringBuffer("");
    String paramval;
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        if (param != null) {
            if (paramString.length() > 0) {
                paramString.append("&");
            }
            System.out.println( "post key : " + param.getKey());
            String value;
            try {
                paramval = param.getValue();
                if(paramval!=null)
                    value = URLEncoder.encode(paramval, "UTF-8");
                else
                    value = "";
            } catch (UnsupportedEncodingException e) {
                value = "";
                e.printStackTrace();
            }


                paramString.append(param.getKey()).append("=")
                        .append(value);
        }
    }
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(endpoint);
        String data = "";

        try {
            // Add your data
            // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))
            //httppost.addHeader("Host", host);
            httppost.addHeader("Content-Type",
                    "application/x-www-form-urlencoded");

            if (!paramString.equals("")) {
                if (tempBuffer.length() > 0) {
                    data = data + tempBuffer.toString();
                }
                data = data + paramString.toString();

                if (data.endsWith("&")) {
                    data = data.substring(0, data.length() - 1);
                } 

                httppost.setEntity(new ByteArrayEntity(data.getBytes()));
            }

            System.out.println( "post Stringbuffer  : " + data);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            int statuscode = response.getStatusLine().getStatusCode();
            System.out.println("Response code : " + statuscode);
            if (statuscode != 200) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            InputStream in = null;
            if (entity != null) {
                in = entity.getContent();
            }

            if (in != null) {
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in, "UTF-8"));
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } finally {
                    in.close();
                }

                String response2 = builder.toString();

                System.out.println("response :" + response2);
                retrycount = 0;

                return response2;
            }
        }
        catch(UnknownHostException e){
            e.printStackTrace();
            return null;
        }
        catch (EOFException eof) {
            if (retrycount < max_retry) {
                eof.printStackTrace();
                post( endpoint, params);
                retrycount = 1;
            }
        } catch (Throwable th) {
            throw new IOException("Error in posting :" + th.getMessage());
        }
        retrycount = 0;
        return null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

请帮帮我

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    您是否确保传递给/token的重定向uri与传递给/authorize的重定向uri相同

    我相信,如果您可以使用Postman工具使用当前的客户机id、secret和scope测试OAuth身份验证代码流,以排除错误的配置,这将有所帮助