XF 2.1 API: create user

Hi folks,

This is my first attempt at the api. I'm trying to create a user which has an endpoint in the docs. I created a super admin key, and here's my code:

Code:
$data_string = ['username'=>'zoxoo1', 'email'=>'zoo@example.com', 'password'=>'1234567'];

    $service_url = 'https://forums.example.com/api/users/';
    $curl = curl_init($service_url);
    $data_string = json_encode($data_string);
   
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'XF-Api-Key: xxxxxxxxxxxxxxxxxxxxx',
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
   
    $curl_response = curl_exec($curl);
    $result = json_decode($curl_response, true);

        if(curl_exec($curl) === false)
        {
            echo 'Curl error: ' . curl_error($curl);
        }
        else
        {
            echo 'Operation completed without any errors';
        }

    echo '<pre>';
    print_r($result);
    echo '</pre>';

curl_close($curl);
unset($curl);

This is returning the following error:

Code:
    [errors] => Array
        (
            [0] => Array
                (
                    [code] => do_not_have_permission
                    [message] => You do not have permission to view this page or perform this action.
                    [params] => Array
                        (
                        )
                )
        )

Is there something wrong with my request, or did I miss something that would provide me with the permissions I need? Thanks for your help!
 
I had the wrong encoding in the header (should be application/x-www-form-urlencoded) and I missed the api_bypass_permissions in the request body.

api_bypass_permissions=>1

With those two changes it worked perfectly. Reading the docs carefully will save you from embarrassing posts... like this one.
 
Back
Top Bottom