C#应用调用Microsoft Graph API无法注册设备到MS Intune的问题
解决C#调用Graph API注册Intune Autopilot设备的OData路由错误
问题根源
你遇到的No OData route exists错误,核心原因是直接调用WindowsAutopilotDeviceIdentities.PostAsync创建设备的方式不符合Graph API的规范。Autopilot设备注册必须通过批量导入接口(ImportDeviceIdentityList),哪怕只注册单台设备也需使用该批量接口,而非直接POST单个WindowsAutopilotDeviceIdentity对象。
解决方案
替换原注册逻辑,使用graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.ImportDeviceIdentityList.PostAsync()方法,构造批量导入请求体完成设备注册。
修改后的完整代码示例
namespace IntuneTest { public partial class MainWindow : Window { private GraphServiceClient graphClient; public MainWindow() { InitializeComponent(); _ = InitializeGraphClient(); // 避免async void的潜在问题 } // Initialize Graph Client with ClientSecretCredential for authentication private async Task InitializeGraphClient() { var credential = new ClientSecretCredential( "TenantID", // 替换为你的租户ID "AppID", // 替换为你的应用ID "ClientSecret" // 替换为你的应用密钥 ); graphClient = new GraphServiceClient(credential, new[] { "https://graph.microsoft.com/.default" }); var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://graph.microsoft.com/.default" }), CancellationToken.None); Debug.WriteLine($"Access Token: {token.Token}"); } // Register a Windows Autopilot device private async Task RegisterDeviceAsync() { try { // 构造单台设备的导入对象(批量接口支持多台) var deviceToImport = new WindowsAutopilotDeviceIdentityImport { SerialNumber = "6WX8WT2", Manufacturer = "Dell Inc.", Model = "Latitude 5590" // 无需手动设置EnrollmentState,导入后由Intune自动处理状态 }; // 构造批量导入请求体 var importRequest = new WindowsAutopilotDeviceIdentityImportList { Devices = new List<WindowsAutopilotDeviceIdentityImport> { deviceToImport } }; // 调用官方指定的批量导入接口 await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.ImportDeviceIdentityList.PostAsync(importRequest); Debug.WriteLine("设备注册请求已提交"); MessageBox.Show("设备注册请求已提交,请等待Intune后台处理!", "成功"); } catch (ServiceException ex) { Debug.WriteLine($"错误信息: {ex.Message}\n内部异常: {ex.InnerException?.Message}"); MessageBox.Show($"注册失败: {ex.Message}", "错误"); } } // Triggered by button click to register the device private async void Button_Click(object sender, RoutedEventArgs e) { await RegisterDeviceAsync(); } } }
补充说明
- PowerShell能正常运行是因为其底层调用的正是该批量导入接口(如
Import-AutopilotDevicecmdlet),而非直接POST单个设备对象。 - 导入后Intune需要数分钟处理请求,可通过Graph API查询
WindowsAutopilotDeviceIdentities集合或在Intune控制台查看设备状态。 - 你已配置的
DeviceManagementServiceConfig.ReadWrite.All权限完全满足该接口的调用要求。
内容的提问来源于stack exchange,提问作者Zdeneik
相关产品推荐
相关产品推荐

