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