rest_cannot_create_user - Sorry, you are not allowed to create new users. CURL WORDPRESS REST API

Viewed 20
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/wordpress/wp-json/wp/v2/users/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"jane\",\"password\":\"secret\",\"email\":\"jane@example.com\"}");
curl_setopt($ch, CURLOPT_USERPWD, 'restapi_newuser' . ':' . 'ZAFA nAe4 uXM7 XN5c Dq8g CSpw');

$headers = array();
$headers[] = 'Content-Type: application/json';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

echo $result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

message i get is

{"code":"rest_cannot_create_user","message":"Sorry, you are not allowed to create new users.","data":{"status":401}}

I am using XAMPP / wp 6.0.2 / Application Passwords (under the user section)

this is a standalone script that just needs to create a new user it need not check if a user exists etc etc. I feel that i am missing out on something ? Any ideas what i am doing wrong ?

just wanted to ad that the I have tried this code with admin as an user and have created an user that can create users and read and write. it fails at line 512 of class-wp-rests-users-conroller.php

if ( ! current_user_can( 'create_users' ) ) {

if comment out the check the curl statement works perfectly. not too sure why it fails checking permission specifically when permission has been granted.

thanks

0 Answers
Related