Skip to content

Accessing Moodle API with an Authorised User

Download PDF

Recently I’ve taken on a project that required a user to register as a Moodle user, assign a theme and enrol in a course based on an enrolment key. All from outside of Moodle and then redirect TO the Moodle instance. The best way to do this was to use the API. Having not used it before(and not coding for a while), I started to look around on how to do it. I had a local instance of Moodle 3.1 setup and was doing all development locally.

I was getting all sorts of errors including authorisations, access, token etc. I thought I’d set everything up correctly in Moodle, so it was getting pretty frustrating.

After a day of searching and trying all sorts of different approaches, I stumbled across this website http://www.spanidis.eu/?p=27, so all credit below goes to Pavlos Spanidis. This was easy to follow and I was able to create a user and access via the API in about 10 minutes.

Thought this may be useful to anyone else that needs assistance with accessing the Moodle API

 

1) Create the user which will serve the web services. Go to:
Site Administrator > Users > Accounts > Add new user
Username: wsuser (user your own)
Password: (user your own)
Fill in the required fields.2) Give the wsuser the appropriate role. Go to:
Site Administrator > Users > Permissions > Define roles
press the Add new role button
Select Role archetype = ARCHETYPE: Manager and press continue
On the form:
Web Service User
Context types where this role may be assigned: System/User/Category/Course
Leave the rest selections as they appear.
Capabilities
Add the following capabilities
webservice/rest:use
moodle/user:create
enrol/manual:enrol
enrol/category:synchronised
enrol/manual:unenrolself
Press Create this role button.

3) Assign System roles. Go to:
Site Administrator > Users > Permissions > Assign System Roles
Assign wsuser user.

4) Create the needed services. Go to:
Site Administrator > Plugins > Web Services > External Services
We need the two services below:
a) Create user – service
On custom services table press add.
Name: create_user
Short: create_user
Enabled should be checked
Authorized users only should be checked
Press show more
Search for capability moodle/user:create and select it.
Press add service.
This service has no functions
Add functions
Search for the core_user_create_user
Select it and press add function.
b) Enroll user – service
On custom services table press add.
Name: enrol_user
Short: enrol_user
Enabled should be checked
Authorized users only should be checked
Press show more
Search for capability enroll/manual:enrol and select it.
Press add service
This service has no functions
Add functions
Search for the enrol_manual_enrol_users
Select it and press add function.
On the list of custom services you have to add the authorized user to be the wsuser.

5) Create the Tokens. Go to:
Site Administrator > Plugins > Web Services > External Services > Manage Tokens
Add
a) Create User
Select user Web Service User
Service – create user
Save changes.
b) Enroll User
Select user Web Service User
Service – enrol user
Save changes.
You now have the two required tokens.

6) All web service protocols are disabled. The «Enable web services» setting can be found in Advanced features. Go to:
• Site Administrator > Advanced features > Enable web services checked – Save.
• Site Administrator > Plugins > Web Services > External Services > Manage Protocols.
Activate REST protocol.
Check the documentation box.

Published ineLearningMoodle

Be First to Comment

Leave a Reply

Your email address will not be published.

*