Questions about token.php and database shortname.

Questions about token.php and database shortname.

Alexander Wiklund發表於
Number of replies: 12

Hello!

I've been trying to get the token authentication working for some time now, but it seems I've hit a hurdle that just won't budge. 

I have set up the web service as instructed in the overview under "Users as clients with token", and I intend to get the token from token.php as the user tries to access the client. When creating a token in managing tokens and using it with the web service test client everything works fine, but as soon as I call token.php I get an error:

"Web service is not available (it doesn't exist or might be disabled)"

And tells me it's occuring at line 76 in token.php, at which the comment states that it "will throw exception if no token found". I thought this could be because each user had to create their token before it could be used, but looking at the security keys page of the user account I'm using it does have a token for the web service. 

I've also been wondering if this could be because of an error in the database, which I have edited to add the shortname for the service. But the documentation seems a bit ambigous at times. In the description for the external_services table (http://docs.moodle.org/dev/External_services_description) I find no reference to the shortname I'm supposed to be adding, which is needed to use token.php if I'm not mistaken. I have added a record anyway just to see if it would work but to no avail.

What could I be doing wrong here?

 

Regards,

Alexander Wiklund

評比平均分數: -
In reply to Alexander Wiklund

Re: Questions about token.php and database shortname.

Alexander Wiklund發表於

I'm not fond of double-posting, but apparently I just missed the edit window and I thought it could be good for you to know what system I'm using:

Moodle 2.2.2+ (Build: 20120315), MySQL 5.1.41, PHP 5.3.2, Apache 2.2.14, all of them running on Ubuntu 10.04.

If I'm lacking any details needed to solve this then feel free to ask!

In reply to Alexander Wiklund

Re: Questions about token.php and database shortname.

Sergio Hernández發表於

Hi

Example

moodle/login/token.php?username=someusername&password=somepass&service=M3D

The shortname in the database is M3D.

Regards

Sergio

In reply to Alexander Wiklund

Re: Questions about token.php and database shortname.

Markus Speissegger發表於

Hi Alexander,

I was running into the same problems a few days ago, and I got them solved. So, maybe this helps:

For using token.php you have to add the shortname to the previously created service manually: Edit the table mdl_external_services (using phpMyAdmin), find the record containing your service (probably id #2), and change the attribute/field shortname to a unique name of your choice.
After that, token.php should return the token you created for the webservice user.

Instead of calling token.php every time, you can use the created token directly to call your server, eg.
http://<FQDN of your web server>/webservice/soap/server.php?wsdl=1&wstoken=<add your token here>

See also Jeromes sample client code at https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-SOAP/client.php


Hope this helps

Markus

評比平均分數:Useful (1)
In reply to Markus Speissegger

Re: Questions about token.php and database shortname.

Alexander Wiklund發表於

Hello again, and thank you for your prompt replies!

I would like to clarify that I don't want to have one web service user with its own token, I would like each user to have their own to use when they log into the system (i.e. fetch it when they log in). It looks like this would be possible following the second set of steps for setup in the web service overview tab, which I am following. I'm also using the REST protocol to try out the web service.

Markus, your reply did spark a thought in me though; is it necessary to have a custom plugin for the web service, even though you only want to use the already available functions that come with Moodle? Now that I think about it my mdl_external_services table didn't contain anything when I edited it, I added the whole row myself. Could it be that I need to add a plugin and trigger an update through the notifications tab for it to be added to the database? I didn't think this would be a problem as I only want to use the built-in web server functions, but since the actuall service is a custom one maybe it is still required?

And while I'm on it, why doesn't the MoodleDocs about the external_services table say anything about a shortname, if it is required to use the service in this way?

A lot of questions in this reply, I hope I didn't create more confusion!

Regards,

Alexander Wiklund

In reply to Alexander Wiklund

Re: Questions about token.php and database shortname.

Jérôme Mouneyrac發表於
Hi Alexander,
I invite you to vote on MDL-29807 ;) Just to confirm, is your service enable? Otherwise it is your service shortname that is wrong (if the error message is not wrong itself). The previous answers from Sergio and Markus seem correct to me.

It is not necessary to create a plugin if all functions that you need already exists. You can either enable the pre-build mobile service, and then call the token.php script with shortname => moodle_mobile_app. Either create you own service (enable it) and edit the shortname into the database.
In reply to Jérôme Mouneyrac

Re: Questions about token.php and database shortname.

Alexander Wiklund發表於

Hello Jerome,

My service is enabled (both in Moodle and in the database) as far as I can tell. I do have an underscore in my shortname, but seeing as the pre-build mobile service also uses this I don't think it should be an issue. Maybe someone with a working web service could be kind and post a row from their mdl_external_service for reference? I really think the issue is with the database.

My only other concern is that when I created the web service it did not add any records to the database at all; I had to add the whole record manually. Could that be of any significance? 

I will try out the pre-build web service tomorrow and let you know how it goes.

Regards,

Alexander Wiklund

In reply to Alexander Wiklund

Re: Questions about token.php and database shortname.

Jérôme Mouneyrac發表於
Hi Alexander,
if you created a service from the admin, it will definitively been stored in the database. Are you looking to the right database?
In reply to Jérôme Mouneyrac

Re: Questions about token.php and database shortname.

Alexander Wiklund發表於

Hello Jerome,

Of course! I had made a backup of the database some time ago, and when I went in to edit the table I had forgotten that I had changed the name of it. I can now retrieve the token successfully. Thank you all for your help!

 

Regards,

Alexander Wiklund

In reply to Markus Speissegger

Re: Questions about token.php and database shortname.

Neeraj Mishra發表於

Hi Markus,

Can u please tell me where to find mdl_external_services table. Also, how can i use phpMyadmin to enter the shortname in table. I have been searching for that table in moodle directory but all efforts are going in vain. Please help.

In reply to Neeraj Mishra

Re: Questions about token.php and database shortname.

Steve Anatai發表於

Not sure how it could be any clearer, but it's possible your phpmyadmin isn't configured correctly.  If you're using phpmyadmin then you should be able to display your moodle database tables by clicking the name of your moodle database.

The table you're looking for is mdl_external_services and after clicking on it you have to select the "Browse" tab.  After that you can select the cell in the "shortname" column against the row that matches your service under "name", and change it to the name of your service.

In reply to Neeraj Mishra

Re: Questions about token.php and database shortname.

Json Chau發表於

you must install phpMyadmin plugin, and then, login, finally, find mdl_external_service table in the left column.

Good look