【问题标题】:I am trying to retrieve users information from Azure active directory using Microsoft Graph api from java code but i am getting 400 error(Bad request)我正在尝试使用 Java 代码中的 Microsoft Graph api 从 Azure 活动目录中检索用户信息,但我收到 400 错误(错误请求)
【发布时间】:2020-08-07 17:38:11
【问题描述】:

我正在尝试使用 Java 代码中的 Microsoft Graph api 从 Azure 活动目录中检索用户信息,但我收到 400 错误(错误请求),但它在邮递员中运行良好。

我正在使用的java代码是

    String url = "https://graph.microsoft.com/v1.0/users?$filter=displayName eq 'Dasari Siri'";
    String token = "Bearer "+accesstoken; 
    URL obj = new URL(url);
    HttpURLConnection con =(HttpURLConnection)obj.openConnection();  
    con.setRequestMethod("GET");
    con.setRequestProperty("Authorization", token);
    con.connect();
    StringBuffer Response = new StringBuffer();
    if(con!=null){
    try {
          BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));        String input ;
           while ((input = br.readLine()) != null){
             Response.append(input);
           }
           br.close();
               }              
           catch (IOException e) {
           e.printStackTrace();
        }catch(Exception e) {
             e.printStackTrace();
          }
}

我得到的错误是

java.io.IOException: Server returned HTTP response code: 400 for URL: https://graph.microsoft.com/v1.0/users?$filter=displayName eq 'Dasari Siri'
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.javatpoint.Downloadcsv.doDownloadCsv(Downloadcsv.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

The postman request which worked fine for me is :

请你看一下,让我知道我在哪里犯了错误

提前谢谢你

【问题讨论】:

  • 能否请您对 url 的过滤器部分进行编码,然后重试?
  • @HuryShen 谢谢你的建议,我对这个很陌生,你能告诉我怎么做吗
  • 您好,请参考我下面提供的解决方案。如果对您的问题有帮助,请accept作为答案(点击我的答案旁边的复选标记,将其从灰色切换为已填充),在此先感谢~

标签: java http microsoft-graph-api httpurlconnection azure-ad-graph-api


【解决方案1】:

您需要对 url 的过滤器部分进行编码并添加一行以将属性“Accept”设置为“application/json”)。请参考我下面的代码:

public class Testgraph {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        String url = "https://graph.microsoft.com/v1.0/users?" + URLEncoder.encode("$filter=displayName eq 'huryTest'", "UTF-8");
        String token = "Bearer " + "your access token"; 
        URL obj = new URL(url);

        HttpURLConnection con =(HttpURLConnection)obj.openConnection();  
        con.setRequestMethod("GET");
        con.setRequestProperty("Authorization", token);
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Content-Type", "application/json");
        int responseCode = con.getResponseCode();

        StringBuffer Response = new StringBuffer();
        if(con!=null){
        try {
              BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));        
              String input ;
               while ((input = br.readLine()) != null){
                 Response.append(input);
                 System.out.print("---------success------");
               }
               br.close();
                   }              
               catch (IOException e) {
               e.printStackTrace();
            }catch(Exception e) {
                 e.printStackTrace();
              }
        }
    }

}

【讨论】:

  • 谢谢@HuryShen 它正在工作,但我有一个小问题希望你能帮我解决问题是我正在获取所有用户的信息,而不是获取指定用户的详细信息只有
  • 是的,我想通了,url应该改成String url="https://graph.microsoft.com/v1.0/users$filter="+URLEncoder.encode("displayName eq 'huryTest'", "UTF-8");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2021-04-22
  • 1970-01-01
  • 2019-12-16
  • 2021-03-27
  • 2022-12-22
  • 1970-01-01
相关资源
最近更新 更多