Creating users with API successful but also not?

Hello,
I wrote a python script which creates 70 test users at once. The code is as follows:

import requests

def get_access_token(client_id, client_secret):
    token_url = "http://localhost:8080/auth/oauth2/token"
    payload = {
        "grant_type": "client_credentials",
        "scope": "profile api"
    }
    response = requests.post(token_url, data=payload, auth=(client_id, client_secret))

    if response.status_code == 200:
        token_data = response.json()
        access_token = token_data.get("access_token")
        return access_token
    else:
        print("Error obtaining access token. Status code:", response.status_code)
        print(response.text)
        return None

def main():
    client_id = "349706746018660355"
    client_secret = "D2rvKUO9NDGKMtrnA5NsDF8yj4EX9jc3K9k0c0p0XqQngV7A76M2KaTSjhbKmqya"

    access_token = get_access_token(client_id, client_secret)
    if access_token:
        url = "http://localhost:8080/api/system/users/"
        jwt = access_token


        for i in range(1, 70 + 1):

            headers = {
                "Authorization": f"Bearer {jwt}",
                "Accept": "application/json",
                "Content-Type": "application/json"
            }

            user_data = {
                "email": f"Test.User{i}@example.com",
                "name": f"Test User{i}",
                "handle": f"TU{i}",
                "kind": f"string{i}",
                "labels": f"string{i}"
            }
            response = requests.post(url, json=user_data, headers=headers)

        if response.status_code == 200:
               print("Users successfully created.")
        else:
                print("Error in the request. Status code:", response.status_code)




main()

and so far the code works, I receive the status code 200 which is right, according to this doc and I’m also using the same keys as mentioned in that doc (however I’m also wondering how one of those created users is supposed to login without a password being set?), BUT even tho I get status code 200, there are no new users anywhere to be seen/found in the users section of the Admin Area, why is that? Did I perhaps miss something?
Any and every help is much appreciated.

Try putting the body within a json.dumps(). That’s what I have for my python requests that are hitting Corteza.

So

user_data = json.dumps({
“email”: f"Test.User{i}@example.com",
“name”: f"Test User{i}“,
“handle”: f"TU{i}”,
“kind”: f"string{i}“,
“labels”: f"string{i}”
})

To set or change the user’s password you’ll use
/system/users/{userid}/password

Nevermind, I just checked and found that while the 70 users are not being listed in the user section, they still do exist.
I used curl and set the password (Thanks for the tip ;D) for one of them using the userID. After setting the password, the user still wasn’t shown in the list, but I tried logging in and it worked.
After logging in the user still wasn’t in the list but at this point that doesn’t matter anymore.

That is bizarre the user doesn’t show up in the user list. Have you checked to see if they are falling within the ‘suspended users’ category?

Anyways, glad you were able to verify they exist and were able to set the password.

image

Yes, I have tried that but still nothing. I’m guessing that users created using the API just don’t show up? Or is that just the case for me? Who knows.

I also tried creating a user using the GUI with the same attributes which didn’t work, I got the “not unique” error, which just confirmed to me that the users exist and are working etc.

So yeah, everything is fine now, thanks :slight_smile:

@shafigh

you were setting the “kind” value, which should be (at least for this list) set as empty.

You can always preview the requests being made from UI in the developer toolbar via Network tab.

So create a user via UI, check the network tab, click on the POST request and view as curl request. You will have a payload in the data and can do similar stuff with it in your script.