从Azure AD获取用户数据时发出问题

时间:2016-03-02 06:22:06

标签: java azure

我正在尝试获取Azure AD中可用的用户。获取时,http 401 Authentication Error遇到并IOException在控制台上捕获。以下是我尝试获取数据的方法 -

public String getUsers(String accessToken) {    

    URL url;       
        String line = "";        
        try {
            System.out.println("\n\n***************Available Users ************\n");

                url = new URL("https://graph.windows.net/" + tenant_id 
                        + "/users");


            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // Set the appropriate header fields in the request header.
            con.setRequestMethod("GET");
            con.setRequestProperty("api-version", "1.6");
            con.setRequestProperty("Authorization", "Bearer " + accessToken);
            con.setRequestProperty("Accept", "application/json;odata=minimalmetadata");

            int responseCode = con.getResponseCode();
            System.out.println("Responsecode : " + responseCode);

        line=HttpClientHelper.getResponseStringFromConn(con,true);
            System.out.println("Line data"+line);


            /*if (responseCode == 200) {
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            } else {
                reader = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            }

            StringBuffer stringBuffer = new StringBuffer();

            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }*/

          System.out.println(line);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return line;
    }   

1 个答案:

答案 0 :(得分:0)

众所周知,您可以参考Error Handling in OAuth 2.0来检查您的问题。

与此同时,正如@GauravMantri所说,您还需要检查Microsoft Graph在AAD申请的许可,请参阅下图。

图1.检查应用程序的许可并添加应用程序 enter image description here

图2.点击Add application并添加Microsoft Graph enter image description here