You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#调用Microsoft Graph API添加许可证时遇Request_BadRequest错误

解决Graph API分配许可证时的"invalid usage location"错误

这个错误的核心原因是:分配许可证的用户必须设置有效的使用位置(UsageLocation),微软Graph API要求该字段为ISO 3166-1 alpha-2格式的国家/地区代码(比如"CN"、"US"、"TR"等),未设置或格式无效都会触发这个报错。

修复方案

方案1:创建用户时直接设置使用位置

修改你的用户创建代码,在User对象中添加UsageLocation属性:

var user = new User
{
    AccountEnabled = true,
    DisplayName = "test",
    MailNickname = "test",
    UserPrincipalName = "test@example.com",
    PasswordProfile = new PasswordProfile
    {
        ForceChangePasswordNextSignIn = true,
        Password = "random.1234"
    },
    UsageLocation = "TR" // 替换为实际的国家/地区代码,比如"CN"代表中国
};

方案2:创建用户后先更新使用位置再分配许可证

如果用户已经创建完成,可在分配许可证前先更新用户的使用位置:

await graphClient.Users
    .Request()
    .AddAsync(user);
Console.WriteLine("Kullanıcı Açıldı");

// 先更新用户的使用位置
var updateUser = new User
{
    UsageLocation = "TR"
};
await graphClient.Users["test@example.com"]
    .Request()
    .UpdateAsync(updateUser);

// 再执行许可证分配
await graphClient.Users["test@example.com"]
    .AssignLicense(addLicenses, removeLicenses)
    .Request()
    .PostAsync();
Console.WriteLine("Lisans Eklendi");

注意事项

  • UsageLocation必须是有效的两位字母代码,不能留空或使用非标准值
  • 部分Office 365许可证受地域限制,设置正确的使用位置才能确保许可证正常分配

内容的提问来源于stack exchange,提问作者Doruk Han

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:50:37