Auth0 Home Blog Docs

Adding role to an user using Authorization API




By using curls calls via Guzzle, I can not assign roles to a user using the “Authorization” extension.

Here is my approach:

  1. Log in and get an authentication Token (No problem):

curl --request POST
–url ‘https://MY_AUTH0_DOMAIN/oauth/token’
–header ‘content-type: application/json’
–data ‘{“grant_type”:“client_credentials”,“scope”:“create:users”,“client_id”: “MY_CLIENT_ID”,“client_secret”: “MY_CLIENT_SECRET”,“audience”: “https://MY_AUTH0_DOMAIN/api/v2/”}’

I get the right access token

  1. Invoke the management API to create the user:
    Send a POST request to https: // MY_AUTH0_DOMAIN / api / v2 / users? with the following parameters:

header: Authorization: Bearer Access Tokden, content-type: application/json

“user_id”: “”,
“connection”: “Username-Password-Authentication”,
“email”: "",
“username”: “xxx”,
“password”: “xxx*”,
“phone_number”: “+xxx”,
“user_metadata”: {“grant_type”:“client_credentials”,“scope”:“create:users”,“client_id”: “MY_CLIENT_ID”,“client_secret”: “MY_CLIENT_SECRET”,“audience”: “https://MY_AUTH0_DOMAIN/api/v2/”},
“email_verified”: false,
“verify_email”: false,
“phone_verified”: false,
“app_metadata”: {}

the user is created and appear in the list of users and in the list of users in Authorization Extension

  1. Now, il need to attribute a role to this user in the Authorization Extension
    a) I’m connecting

$client = new \GuzzleHttp\Client([‘http_errors’ => false]);

  $response = $client->post('https://'.MY_AUTH0_DOMAIN.'/oauth/token', [
      'headers' =>  ['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'],
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => MY_CLIENT_ID,
            'client_secret' => MY_CLIENT_SECRETT, 
            'audience' =>MY_API_AUTHORIZATION_IDENTIFIER,
            'grant_type' => 'client_credentials',

b) The documentation explain that to add a role to an user, this role should be passed as an array
(Add a single user to roles.
PATCH /api/users/59f3520e-8b58-40b6-8971-e939292363ad/roles

[ “484fd082-ce31-495e-92c3-24543d04799c” ]

But none of my attempts pass; I do not see how to pass this table to the API.
Here is the last essay (also in error):

> 	$data=Array( "249c4b43-23cc-484d-b0fa-745a2a292eae");
  //$client2 = new \GuzzleHttp\Client(['http_errors' => false]);
  $client2 = new \GuzzleHttp\Client();
  $response2 = $client2->patch(MY_API_AUTHORIZATION_IDENTIFIER.'users/'.$user_id.'/roles', [
      'headers' =>  [
  		'Content-Type' => 'application/json',
  		'Authorization' => 'Bearer '.$access_token,
        'json' =>$data

It does not work, a format error appears.
Could someone give me some lines of resolution?