Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Microsoft Graph API访问Microsoft Intune中的用户设备_Java_Microsoft Graph Api_Intune - Fatal编程技术网

Java 无法使用Microsoft Graph API访问Microsoft Intune中的用户设备

Java 无法使用Microsoft Graph API访问Microsoft Intune中的用户设备,java,microsoft-graph-api,intune,Java,Microsoft Graph Api,Intune,我正在尝试访问特定用户的托管设备。我已经编写了一个代码,它使用web应用程序获取身份验证代码。我能够看到所有用户以及特定用户。但是,当我尝试访问用户的托管设备时,会出现401个未经授权的错误。我已检查是否已将所有权限授予在azure portal for Microsoft Graph中创建的web应用程序。这是我的密码:- try { String access_token = getAccessToken(); String url_str = "https://graph.

我正在尝试访问特定用户的托管设备。我已经编写了一个代码,它使用web应用程序获取身份验证代码。我能够看到所有用户以及特定用户。但是,当我尝试访问用户的托管设备时,会出现401个未经授权的错误。我已检查是否已将所有权限授予在azure portal for Microsoft Graph中创建的web应用程序。这是我的密码:-

try {
    String access_token = getAccessToken();
    String url_str = "https://graph.microsoft.com/v1.0/users/{user name here}/managedDevices/";

    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();
}
令牌检索代码:-

private String getAccessToken() {
    String accessToken = "";
    try {
        ExecutorService service = Executors.newFixedThreadPool(1); 
        String authorization_url = "https://login.microsoftonline.com/" + Authentication_Constants.TENANT + "/oauth2/authorize/";
        AuthenticationContext authContext = new AuthenticationContext(authorization_url, false, service);
        ClientCredential clientCred = new ClientCredential(Authentication_Constants.CLIENTID, Authentication_Constants.SECRET);
        Future<AuthenticationResult>  future = authContext.acquireToken(Authentication_Constants.RESOURCE, clientCred, null);
        AuthenticationResult authResult = future.get();
        accessToken = authResult.getAccessToken();
    } catch (Exception ex) {
        System.out.println(ex.getLocalizedMessage());
    }
    return accessToken;
}
私有字符串getAccessToken(){
字符串accessToken=“”;
试一试{
ExecutorService=Executors.newFixedThreadPool(1);
字符串授权\u url=”https://login.microsoftonline.com/“+Authentication_Constants.TENANT+”/oauth2/authorize/”;
AuthenticationContext authContext=新的AuthenticationContext(授权url,false,服务);
ClientCredential clientCred=新的ClientCredential(Authentication\u Constants.CLIENTID,Authentication\u Constants.SECRET);
Future-Future=authContext.acquireToken(Authentication\u Constants.RESOURCE,clientCred,null);
AuthenticationResult authResult=future.get();
accessToken=authResult.getAccessToken();
}捕获(例外情况除外){
System.out.println(例如getLocalizedMessage());
}
返回accessToken;
}

我有什么遗漏吗?谢谢

我在Microsoft Intune团队工作,特别是在Microsoft Intune和Microsoft Graph之间的集成方面

从您上面给出的代码的外观来看,您似乎正在尝试使用仅应用程序凭据访问API,目前Microsoft Intune API仅支持使用app+用户凭据(即授权权限)。为了访问这些API,您需要以用户身份进行身份验证

如果您看一下,所有权限都列为需要app+用户凭据的委派权限

如果您需要仅应用程序访问Intune API,我建议您在下面的页面上添加对您的场景的评论

谢谢


Peter

您能提供访问令牌的内容以及如何检索该令牌的示例吗?@marclafler MSFT我提供了如何检索令牌的代码。访问令牌太大,无法在此添加。我正在使用以下url,其中管理员授予我的应用程序许可。可以吗?如果没有,请给我指一些链接,以便正确授权graph访问Intune API。谢谢!