See api-docs Menu

Assign Role to User

Assign one or more existing roles to a user.

To get a list of role IDs that are currently assigned to a user, use the Get Roles for a User API.

To remove one or more roles from a user, use the Remove Role from User API.

Resource URL

https://api.<us_or_eu>.onelogin.com/api/1/users/:id/
add_roles

Header Parameters

Authorization

required

string

Set to bearer:<access_token>.

Set <access_token> to the access token you generated using the Generate Token API.

The access token must have been generated using an API credential pair created using the scope required to call this API. This API can be called using the Manage All scope.

Content-Type

required

string

Set to application/json.

Resource Parameter

id

required

string

Set to the id of the user to which you want to assign a role. If you don’t know the user’s id, use the Get Users API call to return all users and their id values.

Request Parameter

role_id_array

required

array of integers

Set to an array of one or more role IDs. The IDs must be positive integers.

For a list of role IDs available for an account, use the Get Roles API.

Request Body

{
   "role_id_array":[
      123456,
      78910
   ]
}

Sample Response

{
    "status": {
        "type": "success",
        "code": 200,
        "message": "Success",
        "error": false
    }
}

Here are a few different errors that will return a 400 Bad Request status code.

{
    "status": {
        "code": 400,
        "error": true,
        "message": "role_id_array should be a subset of -> [109999, 109777, 103333, 74666]",
        "type": "bad request"
    }
}

{
    "status": {
        "code": 400,
        "error": true,
        "message": "role_id_array is empty",
        "type": "bad request"
    }
}

{
    "status": {
        "code": 400,
        "error": true,
        "message": {
            "attribute": "role_id_array",
            "description": "role_id_array has incorrect data type. It should be -> array"
        },
        "type": "bad request"
    }
}

The following error means that the role is already assigned to the user.

{
    "status": {
        "error": true,
        "code": 400,
        "type": "bad request",
        "message": "input role_id_array is already included as user roles"
    }
}

Aside from more obvious JSON formatting errors, the following error may also mean that your input JSON contain non-integers. The role ID values in the input JSON must be expressed as an array of integers.

{
    "status": {
        "error": true,
        "code": 400,
        "type": "bad request",
        "message": "Input JSON is not valid"
    }
}

Typically, the following error means that the Authorization header is missing or incorrectly formatted. The Authorization header should use this format: bearer:<access_token>.

{
    "status": {
        "error": true,
        "code": 400,
        "type": "bad request",
        "message": "Authorization Information is incorrect"
 }
}

{
    "status": {
        "error": true,
        "code": 400,
        "type": "bad request",
        "message": "Content Type is not specified or specified incorrectly. 
                    Content-Type header must be set to application/json"
    }
}

Typically, the following error means that your user ID value is incorrectly formatted. It must be an integer.

{
    "status": {
        "error": true,
        "code": 400,
        "type": "bad request",
        "message": {
            "attribute": "id",
            "description": "ID is incorrect"
        }
    }
}

Typically, this error means that the access token is invalid.

{
  "status": {
    "error": true,
    "code": 401,
    "type": "Unauthorized",
    "message": "Authentication Failure"
 }
}

Typically, the following error means that the access token used to make the call was generated using API credentials that have insufficient permissions. This API can be called using the Manage All scope only.

{
    "status": {
        "error": true,
        "code": 401,
        "type": "Unauthorized",
        "message": "Insufficient Permission"
    }
}
{
    "status": {
        "error": true,
        "code": 403,
        "type": "forbidden",
        "message": "user is not authorized to access this User"
 }
}
{
    "status": {
        "error": true,
        "code": 404,
        "type": "not found",
        "message": "User for id 11111111 was not found"
 }
}

Postman Collection

Replace sample variables indicated by < > in the sample request body with your actual values. Also, be sure to set Postman-specific environment variables indicated by {{ }}.

Download for the Users API

Sample Code

cURL

Replace sample values indicated by < > with your actual values.

curl 'https://api.<us_or_eu>.onelogin.com/api/1/users/<user_id>/add_roles' \
-X PUT \
-H "Authorization: bearer: <access_token>" \
-H "Content-Type: application/json" \
-d '{ 
    "role_id_array":[ 
        <role_id>, <role_id> 
    ] 
}'

Python

See Work with OAuth 2.0 Tokens, Users, and Roles.


Have a Question?

Have a how-to question? Seeing a weird error? Ask us about it on StackOverflow.

Found a bug? Submit a support ticket.

Have a product idea or request? Share it with us in our Ideas Portal.