Roblox group API issues *Python*

Howdy. I’m having some trouble with roblox’s group API - specifically, to change rank.
I’m using the /v1/groups/{groupId}/users/{userId} API, which requires me to provide a roleId. However, when I send the request (using the patch method), I get a status code 400, reason code 2, which states
the roleset is not found. (I’ve already supplied my roblosecurity and x-csrf token, that’s not the issue.)
However, I know for a fact that the roleId is correct - I’m passing in 249 (as an int), which corresponds to the rank “Corporal” in my group.

                            rankdict = {"Commander":255,"Deputy Commander":254,"Board of Directors":253,"Captain":252,"Lieutenant":251,"Sergeant":250,"Corporal":249,"Senior Public Safety Officer":248,"Public Safety Officer":247,"Suspended":246}

                            def rbx_request(method, url, **kwargs):
                                request = self.session.request(method, url, **kwargs)
                                method = method.lower()
                                if (method == "post") or (method == "put") or (method == "patch") or (method == "delete"):
                                    if "X-CSRF-TOKEN" in request.headers:
                                        self.session.headers["X-CSRF-TOKEN"] = request.headers["X-CSRF-TOKEN"]
                                        if request.status_code == 403:  # Request failed, send it again
                                            request = self.session.request(method, url, **kwargs)
                                return request

                            role_id = int(rankdict[(rank.content).capitalize()])
                            req = rbx_request("PATCH",f"https://groups.roblox.com/v1/groups/11062645/users/{self.userId}", params={"roleId":role_id, "groupId":11062645, "userId":self.userId})

Any help is appreciated.
tl;dr - getting status code 400 reason code 2 on roblox change group rank API call, have no clue why because the role id I’m passing to roblox is valid

edit: here is the API to save you some trouble
https://groups.roblox.com/docs#!/Membership/patch_v1_groups_groupId_users_userId

This

params={"roleId":role_id, "groupId":11062645, "userId":self.userId}

should not include the groupId and userId since those are only meant to be provided in the path.

The following would be correct:

params={"roleId": role_id}

Changed that. Still seeing status code 400 and the rank isn’t changing in the group.
I tried using the roleSetIds as well, which didn’t work either

I’m not entirely sure since I no longer use Python a lot but doesn’t **kwargs automatically wrap everything in a table? If so, you should change this line

request = self.session.request(method, url, **kwargs)

to

request = self.session.request(method, url, kwargs["params"])

Forgot to reply @ibm03

Tried doing that, still didn’t work. I changed it from kwargs to just a param arg and that didn’t work either.
I honestly have no clue at this point, I’m running out of ideas haha

rankdict = {"Commander":255,"Deputy Commander":254,"Board of Directors":253,"Captain":252,"Lieutenant":251,"Sergeant":250,"Corporal":249,"Senior Public Safety Officer":248,"Public Safety Officer":247,"Suspended":246}
	
def rbx_request(method, url, **kwargs):
	request = self.session.request(method, url, kwargs['params'])
	method = method.lower()
	if (method == "post") or (method == "put") or (method == "patch") or (method == "delete"):
		if "X-CSRF-TOKEN" in request.headers:
			self.session.headers["X-CSRF-TOKEN"] = request.headers["X-CSRF-TOKEN"]
			if request.status_code == 403:  # Request failed, send it again
				request = self.session.request(method, url, kwargs['params'])
	return request

role_id = int(rankdict[(rank.content).capitalize()])
req = rbx_request("PATCH",f"https://groups.roblox.com/v1/groups/11062645/users/{self.userId}", params={"roleId":role_id})

This code should work fine. If you’re still having problems, then perhaps it’s some other bit of code.

In that case, I’m not entirely sure. That’s all the code related to that API call, and it’s still not working after changes. There’s really no more information that I can provide other than what’s in the original post. Perhaps somebody else can think of something

I updated the post again, I missed the first request and changed the 403 code detector to detect any 4xx error.

Hello! I wrote the original tutorial that this rbx_request function came from; it was designed to only send an extra request when a 403 code occurred as that is the only response code that requires sending duplicate requests. Adding this change could cause duplicate requests to be sent when they aren’t required - as such you should not modify the function in this way.

1 Like

The value you’re supplying as roleId is not a role id. It’s a rank. You need to send a request to https://groups.roblox.com/v1/groups/{groupId}/roles to obtain a roleId.

solved (thanks @local_ip for helping me in his server!)
because the request method is PATCH, I need to use “json”, not “params” when passing the role_id.

ie:

req = rbx_request("PATCH",f"https://groups.roblox.com/v1/groups/GROUPID/users/{self.userId}", json={"roleId": role_id})