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

使用.NET Google Admin SDK设置用户Building ID时抛出必填参数异常

解决.NET Google Admin SDK设置Building ID时的BadRequest异常

问题描述

我正尝试使用.NET Google Admin SDK为新用户设置Building ID,通过Locations属性进行操作。

Google管理控制台中的Building ID为「用户信息」下「员工信息」子项的自定义字段,示例值为「Great Building」。

使用的代码:

var newUser = new User()
{
    // ...
};

// ...

newUser.Locations = new List<UserLocation>()
{
    new()
    {
        BuildingId = command.BuildingId,
    }
};

// ...

var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();

执行时抛出Google.GoogleApiException异常:

服务admin抛出异常,HttpStatusCode为BadRequest。必填参数:[resource.location.field[0].Value]

问题原因

你误用了SDK的属性:

  • Locations是Google原生办公地点字段的容器,用于设置城市、国家等系统内置地点信息,并非自定义员工字段的配置入口。
  • 截图中的「Building ID」属于自定义员工信息架构(Custom Schema),必须通过CustomSchemas属性配置,而非Locations。

正确代码实现

假设你在管理控制台创建的自定义架构名称为「员工信息」,字段名为「Building id」(名称需与控制台完全一致,包括大小写、空格),修改代码如下:

var newUser = new User()
{
    // 填写用户必填属性,如Name、PrimaryEmail等
};

// 配置自定义员工字段
newUser.CustomSchemas = new Dictionary<string, IDictionary<string, object>>
{
    {
        "员工信息", // 自定义架构名称
        new Dictionary<string, object>
        {
            { "Building id", command.BuildingId } // 自定义字段名
        }
    }
};

// 执行用户插入
var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();

补充说明

  1. 名称匹配要求:自定义架构和字段的名称必须与控制台中创建的完全一致,否则字段无法正确映射。
  2. 原生地点字段的正确用法(若需使用):
    如果你的Building ID是Google原生支持的地点字段,需按SDK要求填充Fields集合:
newUser.Locations = new List<UserLocation>()
{
    new UserLocation
    {
        Fields = new List<UserLocationCustomField>
        {
            new UserLocationCustomField
            {
                Type = "buildingId",
                Value = command.BuildingId
            }
        }
    }
};

但此方式仅适用于系统内置字段,结合你的截图场景,优先使用自定义架构的实现方式。

内容的提问来源于stack exchange,提问作者Eric Eskildsen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:55:59