I was also experiencing this kind of issue. My problem was, that my plugin was just for teachers, so when somebody is assigned as a teacher I also have to assign a web service user role to him. I solved this by creating a simple DATABASE TRIGGER that inserts another role to mdl_role_assignments.
You can generate a token for user on the fly :
Hope this helps