Integration

Web Service Access Control Exception

If you are creating or using a web service in Moodle and see the following message:

{
    "exception": "webservice_access_exception",
    "errorcode": "accessexception",
    "message": "Access control exception"
}

It can be a bit hard to troubleshoot. Generally there is a 4th item with debugging info, but this will only appear if you have the correct Moodle debugging level set:

Site administration > Development > Debugging

At minimum you’ll need to set debug level to NORMAL: Show errors, warnings and notices. That is, it won’t apear if you have it set to NONE or MINIMAL.

Once you do that you should get that 4th parameter:

{
    "exception": "webservice_access_exception",
    "errorcode": "accessexception",
    "message": "Access control exception",
    "debuginfo": "You are not allowed to use the {$a} protocol (missing capability: webservice/rest:use)"
}

In this example, the user that is associated with the web service token didn’t have the capability webservice/rest:use in their system role. Adding the capability to the role fixed the issue.

WSDL for a SOAP based Web Service

When you enable SOAP based web services through Moodle:

Site administration > Plugins > Web Services > Overview
(follow the wizard)

You can access the WSDL for the SOAP based service by passing the flag &wsdl=1 in your URL e.g.

https://{yourmoodle.com}/webservice/soap/server.php?wsdl=1&wstoken=tokenifrequired

Undefined Property $webserviceprotocols

When this error occurs there is no result from the web service. In the PHP error log the following appears:

Undefined property: stdClass::$webserviceprotocols

Check your web service protocols and ensure that the appropriate protocol is enabled.

Site administration > Plugins > Web services > Manage protocols.

Web Service Course or Activity not Accessible

If you are using a web service function like enrol_manual_enrol_users to enrol users into a course, you may come across the following exception:

{
    "exception": "require_login_exception",
    "errorcode": "requireloginerror",
    "message": "Course or activity not accessible."
}

This can happen even if you have put the user in a role with the enrol/manual:enrol capability.

Generally it means the user needs to also have access at the course context, i.e. to have a role in the course that allows them to enrol other users. This might be set per course in the Course Participants area, or it might be assigned at a system or course category context.

Web Service Responses in JSON Format

To get web service responses in JSON format (e.g. with the REST protocol), you can pass the query parameter moodlewsrestformat=json. The default convention with Moodle is return web service responses in XML format.

E.g. you might a URL that looks like this (broken into segments for clarity):

https://yourmoodle.com/webservice/rest/server.php?
wstoken=<token>
&wsfunction=core_webservice_get_site_info
&moodlewsrestformat=json

This one is calling the core_webservice_get_site_info function with a token, and specifying that the output format needs to be JSON.