Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login API via webservice in moodle

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.

like image 384
neha sk Avatar asked Sep 13 '25 22:09

neha sk


1 Answers

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.

like image 63
dannielarriola Avatar answered Sep 16 '25 13:09

dannielarriola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!