Google 目录服务更新密码 400 密码无效

Google directory service update password 400 Invalid password

我正在尝试使用 Google 目录服务更新用户,但出现错误:“400 密码无效”。这是我使用的代码:

var certificate = new X509Certificate2(certificatePath, "notasecret", 

X509KeyStorageFlags.Exportable);
var sai = new ServiceAccountCredential.Initializer(clientId)
{
    Scopes = new[]
                        {
                            DirectoryService.Scope.AdminDirectoryUser,
                            DirectoryService.Scope.AdminDirectoryDomain
                        }
}.FromCertificate(certificate);
sai.User = "admin@domain.com";

ServiceAccountCredential credential = new ServiceAccountCredential(sai);

var directoryService = new DirectoryService(new BaseClientService.Initializer
                                            {
                                                ApplicationName = "Admin",
                                                HttpClientInitializer = credential
                                            });

User user = directoryService.Users.Get("someuser@domain.com").Execute();
user.Password = "SomeP@ssword1234";
directoryService.Users.Update(user, "someuser@2contact.com").Execute(); // Error 400 Invalid password

最后一行抛出错误。 顺便获取现有用户:

var listRequest = _directoryService.Users.List();
listRequest.Domain = "domain.com";
listRequest.MaxResults = 500;
var results = listRequest.Execute(); // Works fine!

我做错了什么?

您是否尝试过不使用特殊字符来进行测试?也许编码有一些问题。 Google 只要求最少的字符数。

另一方面,API 建议在上传前使用哈希加密。

The user's password value is required when creating a user account. It is optional when updating a user and should only be provided if the user is updating their account password. A password can contain any combination of ASCII characters. A minimum of 8 characters is required. The maximum length is 100 characters. We recommend sending the password property value as a base 16 bit, hexidecimal-encoded hash value. If a hashFunction is specified, the password must be a valid hash key.

The password value is never returned in the API's response body.