admin管理员组文章数量:1388105
I'm trying to set a new user's Building id
using the .NET Google Admin SDK. I'm using the Locations
property to do that.
Here's Building id
in the Google Admin Console:
And in my code:
var newUser = new User()
{
// ...
};
// ...
newUser.Locations = new List<UserLocation>()
{
new()
{
BuildingId = command.BuildingId,
}
};
// ...
var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();
The last line throws Google.GoogleApiException
:
The service admin has thrown an exception. HttpStatusCode is BadRequest. Required parameter: [resource.location.field[0].Value]
I'm trying to set a new user's Building id
using the .NET Google Admin SDK. I'm using the Locations
property to do that.
Here's Building id
in the Google Admin Console:
And in my code:
var newUser = new User()
{
// ...
};
// ...
newUser.Locations = new List<UserLocation>()
{
new()
{
BuildingId = command.BuildingId,
}
};
// ...
var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();
The last line throws Google.GoogleApiException
:
Share Improve this question asked Mar 18 at 16:12 Eric EskildsenEric Eskildsen 4,8503 gold badges50 silver badges59 bronze badgesThe service admin has thrown an exception. HttpStatusCode is BadRequest. Required parameter: [resource.location.field[0].Value]
1 Answer
Reset to default 2tl;dr: Set Area and Type to desk
newUser.Locations = new List<UserLocation>()
{
new()
{
Area = "desk",
BuildingId = command.BuildingId,
Type = "desk",
}
};
Explanation
There are two issues with the code in my question:
The location's
Area
property is required. This doesn't seem to be documented.The below change eliminates the exception. However, the building ID doesn't show up in the GUI.
newUser.Locations = new List<UserLocation>() { new() { Area = "desk", BuildingId = command.BuildingId, } };
For the building ID to show up in the GUI,
Type
must also be set. Based on what the GUI setsType
andArea
to when you setBuilding id
there, it looks like both properties need to be set todesk
.With the change below, the building ID now shows up in the GUI under Employee info:
newUser.Locations = new List<UserLocation>() { new() { Area = "desk", BuildingId = command.BuildingId, Type = "desk", } };
Methodology
I discovered this by setting Building id
in the GUI, then getting the user through the API.
In the .NET Google Admin SDK, Locations
is, unfortunately, of type object
. At runtime, you can see it's a Newtonsoft array. I wrote an extension class that I hereby dedicate to the public domain:
using Google.Apis.Admin.Directory.directory_v1.Data;
using Newtonsoft.Json;
namespace Your.Namespace.Here;
public static class UserExtensions
{
public static List<UserLocation> GetLocations(this User user)
{
if (user.Locations is null)
{
return [];
}
return JsonConvert.DeserializeObject<List<UserLocation>>(user.Locations.ToString()!)!;
}
}
本文标签:
版权声明:本文标题:c# - Setting a Google user's Building ID: Locations property throws exception: "Required parameter: [resource.l 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744502915a2609425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论