I'm currently a student at a University and I'm using Moodle everyday. I would like to access some information that is available to me (For example, information of the classes I'm taking, Which assignments are due and when , etc)
I did some research regarding Moodle's API but it all seemed geared toward the power user who actually runs Moodle (My University).
Is there an easy way for me as a student to get the information?
My application uses Node.js
If your university has enabled Web Services for the mobile app, you can generate your own API token and call the Web Services used by the mobile app. If the latter are not enabled, you have to get in touch with your administrator to get Web Services access.
Demo using moodle.org
First, let's get an API token (replace $PASSWORD
with your password):
$ curl -d username="fmcorz" -d password="$PASSWORD" 'https://moodle.org/login/token.php?service=moodle_mobile_app'
{
"token":"SNIPTOKEN",
"privatetoken":"SNIPPRIVATE"
}
Next, we need your userid
, it will be used throughout other web services call. You can obtain your userid
by calling the web service core_webservice_get_site_info
. Make sure to replace $TOKEN
with the token you obtained above.
$ curl -d wstoken="$TOKEN" -d wsfunction=core_webservice_get_site_info 'https://moodle.org/webservice/rest/server.php?moodlewsrestformat=json' | python -m json.tool | grep userid
"userid": 1451616,
Now that you have your userid
, we can request the courses that you are enrolled in.
$ curl -d wstoken="$TOKEN" -d wsfunction=core_enrol_get_users_courses -d userid=1451616 'https://moodle.org/webservice/rest/server.php?moodlewsrestformat=json' | python -m json.tool
[
{
...snip...
"fullname": "Moodle in English",
"id": 5,
...snip...
},
{
...snip...
"fullname": "Moodle en fran\u00e7ais",
"id": 20,
...snip...
},
{
...snip...
"fullname": "Moodle Certification",
"id": 48,
...snip...
}
]
Recap'
Pre-requisites:
- The Mobile App webservices must be enabled
- The REST protocol must be enabled
- You need an API token
Querying:
- Requests are made to
YOURHOST/webservice/rest/server.php?moodlewsrestformat=json
. - Requests must be
POST
requests - Requests must contain
wstoken
: Your token - Requests must contain
wsfunction
: The function you are calling - Requests type must be:
application/x-www-form-urlencoded
More
I've greatly simplified how this works and what alternatives there are, but this should get you started. You will likely be interested in looking at the developer documentation to get more information about the available web services:
- Web Services developer documentation
- Web Services functions list (May not be updated frequently)
来源:https://stackoverflow.com/questions/44652206/getting-information-from-the-moodle-api-as-a-student