Trying to create a user in moodle but all I'm getting is <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<EXCEPTION class=\"moodle_exception\">\n<ERRORCODE>invalidtoken</ERRORCODE>\n<MESSAGE>Invalid token - token not found</MESSAGE>\n</EXCEPTION>\n
The token is absolutely correct because I'm getting it from /admin/settings.php?section=webservicetokens
This is the code I'm using
const axios = require('axios');
var token = 'XXXXXXXXXXXXXxx';
var functionname = 'core_user_create_users';
var userstocreate = [{
username: 'bananaicecream',
password: '1234',
firstname: 'Banana',
lastname: 'Republic',
email: 'test@example.com',
idnumber: 'AUTOGENERATEDID001',
lang: 'en',
description: 'If you die you die',
country: 'us'
}];
export async function callMoodle() {
return axios({
url: `https://somedomainname.moodlecloud.com/webservice/rest/server.php`,
method: 'post',
data: {
wstoken: token,
wsfunction: functionname,
moodlewsrestformat: 'json',
users: userstocreate
}
})
.then( (response) => {
return {status: response.status, response: response.data};
})
.catch( (error) => {
return {status: error.response.status, response: error.response.data};
});
}
Any clue what I'm doing wrong? because i have set up an external service called 'My Service' and assigned the appropriate functions for it as well
Below is the functions