I configured custom OAuth2 service and I am getting error message - Your session has timed out. Please login again.
This got me to look into the codes in /admin/oauth2callback.php(https://github.com/moodle/moodle/blob/master/admin/oauth2callback.php) and I tried to print out the value of "state", it was incomplete that I was getting /auth/oauth2/login.php?wantsurl=https%3A%2F%2Fmoodle.site%2F instead of the entire "state" value which should be /auth/oauth2/login.php?wantsurl=https%3A%2F%2Fmoodle.site%2F&sesskey=QH3jBiWkLc&id=1. This was definitely the encoding issue. Since the oauth2callback.php only uses the state variable, it could not find the session key.
I also found out this happened before from the following link -https://moodle.org/mod/forum/discuss.php?d=370241.
Please help. Thank you.