Search code examples
moodlemoodle-api

How to get Moodle user id through Web Services API using token


Some background

I am connecting external application (server) with Moodle through Web Services API.

  • First a user obtains web services token through a call to /login/token.php.
  • Above token can be used to call web services functions through /webservice/rest/server.php endpoint.

The problem

In some cases I need to provide userid, which I don't know, because /login/token.php does not return it.

Things I've tried

I searched through the Web Services api documentation and found core_session_time_remaining function. Documentation says it returns userid and timeremaining properties:

object {
userid int   //The current user id.
timeremaining int   //The number of seconds remaining in this session.
} 

The problem is, returned userid is always 0, irrelevant of what token was used to call this function. Is it a bug in Moodle, or am I using it wrong? How can I get userid?


Solution

  • did you ever tried using the core_webservice_get_site_info wsfunction?

    I use it and works fine.

    According to the docs: "Return some site info / user info / list web service functions" API Doc