Creating user in a group error (api)

#!/usr/bin/env python3

import requests
import json
import time
import string
from random import *
import sys

ID = [“00dfgdfdfs45t54”]

url = “https://dead.okta.com/api/v1/users?activate=false

payload = “”
headers = {
‘Accept’: “application/json”,
‘Content-Type’: “application/json”,
‘Authorization’: "SSWS “api_token”
}

body = {
‘profile’: {
‘firstName’: “dead”,
‘lastName’: “walker”,
‘email’: “user_name@deadwalker.com”,
‘login’: “user_name@deadwalker.com”,
},

“groupIds”: ID
}

response = requests.post(url, data=payload, headers=headers, json=body)
print(response.text)

Error response I get is “{“errorCode”:“E0000060”,“errorSummary”:“Unsupported operation.”,“errorLink”:“E0000060”,“errorId”:“oaeO8josUT2SziaklKJs8C6bA”,“errorCauses”:[]}”

What am I doing wrong?

Hi @DeadWalker12,

Can you try using postman/curl to make this request?
That should give you an idea if all the request parameters are fine.

Also, can you try creating a user with credentials using your python code?(https://developer.okta.com/docs/reference/api/users/#request-example-3)

Hey @vijet, Thanks, I will try the postman suggestion, and yes, I have been able to make a user using the py code above, including credentials, and expiring the password. I just can’t figure out how to add a group upon creation.

I was unable to figure it out as part of the creation process, so now I add the user to the group immediately after creating the user.

PUT /api/v1/groups/-group id-/users/-user id-

@kevintparker thanks for your input. Can you show me how or where in your code you are executing this? I tried what you suggested and got this:

{“errorCode”:“E0000060”,“errorSummary”:“Unsupported operation.”,“errorLink”:“E0000060”,“errorId”:“oaekE2TJ”,“errorCauses”:[]}

import requests
import json
import time
import string
from random import *
import sys

#Adds user to groups
url = “https://example.okta.com/api/v1/groups/0056789/users/345678

payload = “”
headers = {
‘Accept’: “application/json”,
‘Content-Type’: “application/json”,
‘Authorization’: “SSWS API_Token”
}

response = requests.put(url, data=payload, headers=headers)
print(response.text)

So, from the response you get back from posting to /v1/users?activate=false, look for ‘id’ in the json that is returned. For example, 00asdhedg321 (not real) That is the -user id- I am referring to.

It looks like your -group id- is 00dfgdfdfs45t54,

So you post would be:
PUT /api/v1/groups/00dfgdfdfs45t54/users/00asdhedg321

This is my C# code for a method I have:

private void AssignGroups(string userId, string groupId)
        {
            //ashGroups
            string thisUrl = baseUrl + string.Format("/api/v1/groups/{0}/users/{1}", groupId, userId);

        HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(thisUrl);
        httpWReq.Method = "PUT";

        string data = "";

        httpWReq.Headers.Add("Authorization", string.Format("SSWS {0}", apiKey));

        HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            data = streamReader.ReadToEnd();
        }

    }
1 Like