Error creating new user in Moodle via webservice

Asked

Viewed 1,646 times

3

I have been trying to add a new user to Moodle using your webservice api

I have already modified several times the parameters passed via post, tested using an array instead of an object, tested some changes in the structure of the array that is passed, but always get the same response from Moodle:

"Missing required key in single Structure: users"

The answer:

{
    "exception":"invalid_parameter_exception",
    "errorcode":"invalidparameter",
    "message":"Valor inv\u00e1lido de par\u00e2metro detectado",
    "debuginfo":"Missing required key in single structure: users"
}

My code:

$functionname = 'core_user_create_users';
$user1 = new stdClass();
$user1->id = 1;  //
$user1->username = 'testusername1';
$user1->password = 'testpassword1';
$user1->firstname = 'testfirstname1';
$user1->lastname = 'testlastname1';
$user1->email = '[email protected]';
$user1->auth = 'manual';
$user1->idnumber = 'testidnumber1';
$user1->description = 'Hello World!';
$user1->city = 'testcity1';
$user1->country = 'BR';

$token = 'mytoken';
$domainname = 'localhost/moodle';
$functionname = 'core_user_create_users';
$restformat = 'json';
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname.'&moodlewsrestformat=' . $restformat;

$users = array($user1);
$params = array('users' => $users); 

$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',                    
        'header' => 'Content-Type: text/plain',
        'content' => $params                             
    )
));

$contents = file_get_contents($serverurl, null, $context);            

//print_r($contents);

$resposta = json_decode($contents);  

From what I’ve checked, all mandatory fields are filled in.

I am using a valid token, and the user has the necessary permission to use the function core_user_create_users.

I used the following code as groundwork

UPDATE

Among the common mistakes when trying to create a user via webservice are

  • Try to register required field with empty value

  • Try to register password that does not meet the password policy defined in the Moodle configuration

  • Forget to encapsulate the object(s) (s) with the user(s) information in an array and switch to 'users''

    $users = array($user1, $user2);
    $params = array('users' => $users);
    

2 answers

6


This error may be related to Moodle password policy that by default requires the password to contain:

Minimum number of characters: 8
Minimum number: 1
Minimum capital letters: 1
Lower case minimum: 1
Minimum non-alphanumeric characters: 1

Information can be read here.

Check that you are actually meeting these requirements.


Notes:
These settings may be changed in the policies of the:

Settings Site Administration Security Site policies.

  • I had performed tests with other passwords, $user1->password = 'Password123!'; But I’ll check the security settings though.

  • Just for enrichment: I found this topic in which the problem was very similar. The striking solution was the same as the one suggested by the friend.

  • I even disabled the password policy. But nothing has changed.

  • @Edgarmunizberlinck I had already bumped into this topic previously too...

  • I couldn’t remember exactly how I solved the situation, so I updated the question by mentioning the most common errors I found in the tests I did with the Moodle webservice

2

Solved with Moodle 3.6.2+ (Build: 20190222)

invalidparameter

I went through a similar problem and I will report here. I hope it is useful for someone someday, because it was hard to find when I needed.

I used as a starting point this code here: https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-REST/client.php

What generated "invalidparameter" error was basically this line:

$user1->theme = 'standard';

I was just commenting on the theme line that Moodle almost accepted. I say almost because, when I got rid of the "invalidparameter" error, Moodle released the error "forcepasswordchangenotice"

forcepasswordchangenotice

This error was released because I had not logged in with the user I created to use the webservice. When I signed with him, Moodle asked me to change the password. As soon as I changed the password it unlocked the user to use the webservice, and now it’s OK.

Finally, I was able to create a user by passing these values here:

$rand = rand(1,1000000);
$user1->username = "testusername$rand";
$user1->password = 'Testp@123';
$user1->firstname = "testfirstname";
$user1->lastname = 'testlastname1';
$user1->email = "[email protected]";
$user1->auth = 'manual';
$user1->idnumber = "testidnumber$rand";
$user1->lang = 'pt_br';
$user1->mailformat = 0;
$user1->description = 'Hello World!';
$user1->city = 'testcity1';
$user1->country = 'br';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.