I am trying to code login API from an external website to moodle. I don't know how to achieve it. Please guide me through. I want to create via REST webservice. Such that student logs in via that external website.
Asked
Active
Viewed 7,354 times
1 Answers
6
Ok, first of all, make sure you have well-configured moodle
- web services enabled: YOUR_MOODLE_URL/admin/settings.php?section=optionalsubsystems
- Rest protocol enabled: YOUR_MOODLE_URL/admin/settings.php?section=webserviceprotocols
- Moodle mobile web services enabled: YOUR_MOODLE_URL/admin/settings.php?section=externalservices
- Enable the role capability webservice/rest:use for the Authenticated user role: generally YOUR_MOODLE_URL/admin/roles/define.php?action=edit&roleid=7
Assuming that this refers to the latest version 3.5 here an example with javascript and axios (Axios is a promise based HTTP client for the browser and node.js)
axios
.get(YOUR_MOODLE_URL + "/login/token.php", {
params: {
username: "YOUR_FORM_USERNAME",
password: "YOUR_FORM_PASSWORD",
service: 'moodle_mobile_app'
}
})
.then((response) => {
console.log(response.data)
})
.catch((err) => console.error(error))
Or if you want to test it with the browser go to: YOUR_MOODLE_URL/login/token.php?username=YOUR_FORM_USERNAME&password=YOUR_FORM_PASSWORD&service=moodle_mobile_app
Replace YOUR_MOODLE_URL with the url of your moodle instalation and YOUR_FORM_USERNAME and YOUR_FORM_PASSWORD with valid credentials.
With this you get the user logged token to perform any action in moodle.
dannielarriola
- 84
- 4