我尝试在 Java 上使用 Microsoft Graph。我成功获得了访问令牌。
但是,当我通过 HttpURLConnection 使用此令牌时,我的访问被拒绝并从 Microsoft 服务器捕获了 400 错误。
HttpURLConnection con = null;
String url_str = "https://graph.microsoft.com/v1.0/me";
String bearer_token = "EwA4A8l6BA...";
URL url = new URL(url_str);
con = ( HttpURLConnection )url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("GET");
con.setRequestProperty("Authorization","Bearer " + bearer_token);
con.setRequestProperty("Host","graph.microsoft.com");
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader( con.getInputStream() )); // Error has been occured here.
String str = null;
String line;
while((line = br.readLine()) != null){
str += line;
}
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
这是错误消息。
线程“main”java.io.IOException 中出现异常:服务器返回 HTTP 响应代码:400 对于 …