Search code examples
c#.netgoogle-directory-apigoogle-workspace

Google directory service update password 400 Invalid password


I'm trying to update a user using Google Directory services, but I get an error: "400 Invalid password". Here's the code I use:

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 = "[email protected]";

ServiceAccountCredential credential = new ServiceAccountCredential(sai);

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

User user = directoryService.Users.Get("[email protected]").Execute();
user.Password = "SomeP@ssword1234";
directoryService.Users.Update(user, "[email protected]").Execute(); // Error 400 Invalid password

The last line throws the error. Getting existing users works by the way:

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

What am I doing wrong?


Solution

  • Did you tried without special characters in order to test? Maybe there are some problems about the encoding. Google only request a minimum of characters.

    In the other hand the API recommend to encrypt with a hash before to upload.

    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.