General developer forum

Access control Exception while using webservice

 
Picture of Pooja Kankamwar
Access control Exception while using webservice
 

{"exception":"webservice_access_exception","errorcode":"accessexception","message":"Access control exception","debuginfo":"Access to the function core_user_create_users() is not allowed.\n                     There could be multiple reasons for this:\n                     1. The service linked to the user token does not contain the function.\n                     2. The service is user-restricted and the user is not listed.\n                     3. The service is IP-restricted and the user IP is not listed.\n                     4. The service is time-restricted and the time has expired.\n                     5. The token is time-restricted and the time has expired.\n                     6. The service requires a specific capability which the user does not have.\n                     7. The function is called with username\/password (no user token is sent)\n                     and none of the services has the function to allow the user.\n                     These settings can be found in Administration > Site administration\n                     > Plugins > Web services > External services and Manage tokens."}

I'm having this error . How to resolve? I'm trying to create a user using function
I want users to register via different UI(HTML Template) 
this is my code:- 
PS- the code is just for testing 

//client.php

<?php

include 'curl.php';

$token = 'e2fa6fbbfc020314fa43b19400695338';

$domainname = 'localhost/moodle';

$functionname = 'core_user_create_users';

// REST RETURNED VALUES FORMAT

$restformat = 'json'; //Also possible in Moodle 2.2 and later: 'json'

                     //Setting it to 'json' will fail all calls on earlier Moodle version

//////// moodle_user_create_users ////////

/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION

$user1 = new stdClass();

$user1->username = 'testusername1';

$user1->password = 'testpassword1';

$user1->firstname = 'testfirstname1';

$user1->lastname = 'testlastname1';

$user1->email = 'testemail1@moodle.com';

$user1->auth = 'manual';

$user1->idnumber = 'testidnumber1';

$user1->lang = 'en';

$user1->theme = 'standard';

$user1->timezone = '-12.5';

$user1->mailformat = 0;

$user1->description = 'Hello World!';

$user1->city = 'testcity1';

$user1->country = 'au';

$preferencename1 = 'preference1';

$preferencename2 = 'preference2';

$user1->preferences = array(

    array('type' => $preferencename1, 'value' => 'preferencevalue1'),

    array('type' => $preferencename2, 'value' => 'preferencevalue2'));

$user2 = new stdClass();

$user2->username = 'testusername2';

$user2->password = 'testpassword2';

$user2->firstname = 'testfirstname2';

$user2->lastname = 'testlastname2';

$user2->email = 'testemail2@moodle.com';

$user2->timezone = 'Pacific/Port_Moresby';

$users = array($user1, $user2);

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

/// REST CALL

header('Content-Type: text/plain');

$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;

require_once('./curl.php');

$curl = new curl;

//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2

$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';

$resp = $curl->post($serverurl . $restformat, $params);

print_r($resp);

Please Guide me
Thanks! 

 
Average of ratings: -