gpt4 book ai didi

java - 使用 Azure 门户中的 Web 应用程序凭据访问 Java 中的 Microsoft Graph Rest 服务

转载 作者:行者123 更新时间:2023-12-02 11:38:22 25 4
gpt4 key购买 nike

我正在尝试通过 Microsoft Graph Api 访问以下网址:- https://graph.microsoft.com/v1.0/me我使用了在 stackoverflow 上找到的下面给出的代码,理想情况下它应该给我 JSON。但我在运行代码时遇到异常:-

try {
String url_str = "https://graph.microsoft.com/v1.0/me";
String access_token = getAccessToken();

url = new URL(url_str);
con = ( HttpURLConnection )url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", access_token);
con.setRequestProperty("Accept","application/json");
con.connect();

br = new BufferedReader(new InputStreamReader( con.getInputStream() ));
String str = null;
String line;
while((line = br.readLine()) != null){
str += line;
}
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}

我正在获取有效的访问 token 。但我遇到以下异常:-

java.io.FileNotFoundException: https://graph.microsoft.com/v1.0/me
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.controller.MicrosoftGraphController.getUserInfo(MicrosoftGraphController.java:50)
at com.controller.MicrosoftGraphController.main(MicrosoftGraphController.java:82)

我尝试搜索,但没有找到与此特定问题相关的任何内容。有什么建议么?谢谢!

最佳答案

将代码更改为:

con.setRequestProperty("Authorization", "Bearer " + access_token);

您正在使用 OAuth2 协议(protocol),它需要的不仅仅是原始 token 。请注意代码中“Bearer”一词后面的空格字符。

看看the docs了解更多详情。

关于java - 使用 Azure 门户中的 Web 应用程序凭据访问 Java 中的 Microsoft Graph Rest 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48755076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com