User API - not able to add users

I am trying to use the User API to add users.
I have used it successfully for a long time, but it no longer works recently. I am wondering if something changed.

I am posting my code below.
Could you please review it to see if I am doing anything wrong?
Thank you.

var AddUsers = vaiUsers.Where(p => !kintoneUsers.Any(p2 => p2.employeeNumber == p.WdEmployeeId)).ToList();
var SUBDOMAIN = "example_subdomain";

List<object> NewUsers = new List<object>();

foreach (var item in AddUsers)
{
  NewUsers.Add(new
  {
    code = item.AdUserId.Trim().ToLower(),
    valid = true,
    password = newKintoneUserPassword,
    name = item.FirstName + " " + item.LastName,
    surName = item.LastName,
    givenName = item.FirstName,
    localNameLocale = "en",
    timezone = "America/New_York",
    locale = "en",
    email = item.EmailAddress.ToLower(),
    employeeNumber = item.WdEmployeeId,
    customItemValues = new List<CustomItemValue> { new CustomItemValue { code = "supervisor", value = (from k in kintoneUsers where k.employeeNumber == item.WdManagerId01 select k.id).FirstOrDefault() } }
  });

  string jsonUpdateUsers = "{\"users\":" + JsonConvert.SerializeObject(splitAddUserInKintone) + "}";

  using (var httpClient = new HttpClient())
  {
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://{SUBDOMAIN}.kintone.com/v1/users.json"))
    {
      byte[] toBeEncoded = System.Text.ASCIIEncoding.ASCII.GetBytes("apiUser:" + apiUserPassword);

      var encoded2 = System.Convert.ToBase64String(toBeEncoded);

      request.Headers.TryAddWithoutValidation("X-Cybozu-Authorization", encoded2);

      request.Content = new StringContent(jsonUpdateUsers);
      request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

      var response = await httpClient.SendAsync(request);
    }
  }
}

Hello @djlewis

Welcome to the Kintone Developer Forum & thank you for posting your question!

Housekeeping

To better help you out, please provide more information regarding the following:

  • Your current situation
  • Your desired outcome
  • What you have tried so far
  • Context to your code (language, framework, running environment, etc.)
  • Error message you got (Screenshots are helpful :ok_hand:)

Additional questions specific to your code:

  • What is in the splitAddUserInKintone variable?
  • What is the data format from the WdEmployeeId variable?

Also, we highly recommend you use our question template below to help provide vital information for our community to help you out.

Lastly, I removed your hard-coded subdomain from your post for security reasons and replaced it with the SUBDOMAIN variable.

Question Template


## Question / Problem
_Got an error message?_
_Building a JS customization?_

## Current Situation
_How is your Kintone App configured?_
_What are you trying to do?_

### Code / Attempts
_Share your code and setup_

### Error Message
_Screenshots are helpful_

## Desired Outcome / Expected Behavior
_Screenshots are helpful_

## Referenced Resources
_List the [tutorials](https://kintone.dev/en/tutorials/), [API docs](https://kintone.dev/en/docs/), & [help article](https://get.kintone.help/en/) you read_

Recent Changes to User API?

The most "recent" changes made to the Kintone User API were back in May 2022.

The most recent changes to the Add Users API especially was back in March 2021.

Using Add Users API

I can confirm that the Add Users API is still working as expected. I was able to add a user to my Kintone App using the following API calls:

1. Add User

Add a 'test' user to my Kintone subdomain using the Add Users API.

const body = { users: [ { code: 'test', valid: true, password: 'password', name: 'test' } ] };
kintone.api( kintone.api.url('/v1/users', true), 'POST', body,
  function (resp) { console.log(resp); },
  function (error) { console.log(error); }
);

2. Update User Services

Note that once a user is added with this API, the Update User Services API needs to be used against this user for the user to have access to Kintone services.

const body = { users: [ { code: 'test', services: ['kintone'] } ] };
kintone.api( kintone.api.url('/v1/users/services', true), 'PUT', body,
  function (resp) { console.log(resp); },
  function (error) { console.log(error); }
);

3. Get User Services

To confirm that the user has been added to the Kintone subdomain, use the Get User Services API.

const body = { 'codes': ['test'] };
kintone.api(kintone.api.url('/v1/users/services', true), 'GET', body,
  function(resp) { console.log(resp.users); },
  function(error) { console.log(error); }
);

Common Errors

Here are some common errors that may be relevant:

Authentication

  • Verify the apiUser and apiUserPassword values.
  • Verify that the Basic Authentication header is correct.

JSON Serialization

  • Verify that the serialization process (JsonConvert.SerializeObject) works correctly.

Data Compatibility

  • Verify that the data types and structures used in the code match the actual data you are working with.