Unable to login from phonegap application

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
yonghan's picture

Hi all,currently i'm using drupal 7.29,services 3.5,phonegap 3.1,jquery 1.9.1,jquery mobile 1.3.2 . I cannot login by using the following script from phonegap application but able to login from extenral php file.Thanks a lot.

login.js

dashboard.js

index.html

Comments

Hi First you try login with

asghar's picture

Hi
First you try login with firfox poster or chrome poster and if you successfully login with this senerio then you try it with phonegap. If you have already tried with poster then paste here your response then someone froum community would guide you better.

i have tested with poster

yonghan's picture

hi,i have test by using poster and create php file to test the login code,it works.but when i test by using phonegap with bluestacks or android emulator,it gives me 406 error.i check if i was logged in in the website,but it doesn't logged in.thanks

I had similar issue

monaw's picture

I had similar issue (https://groups.drupal.org/node/358308) with a Titanium mobile app. Forefox poster works but could not get the app to work using the same calls. I ended up downgrading Services to 3.3 which worked.

Good luck! If you solve your problem, please post back the solution (:

I have solve the problem and i can now login and logout

yonghan's picture

Hi monaw,i have solve the problem and i can login and logout now.Here are the codes,

http://pastebin.com/cxaRUs6Z

http://pastebin.com/7vfxBGVn

Hope it helps. :)

Congratulations and thank you

monaw's picture

Congratulations and thank you so much! We'll give it another try (:

You're welcome,hope it works

yonghan's picture

You're welcome,hope it works for you.By the way are you using jqm with titanium studio?I got issues with logout script,it does logout,but it doesn't refresh the page.Perhaps you can help me to point me how to handle it.Thanks

@yonghan thanks man, I'll be

pipep's picture

@yonghan thanks man, I'll be eternally grateful as this worked for me too on drupal 7.26 + services 3.7 + phonegap 3.4 + xcode 5.1 + jquery mobile 1.4.2 + jquery 1.11

You're welcome

yonghan's picture

@pipep I'm glad if the code works for you,and you're welcome. :)

Not sure how this is working for anybody

mastermindg's picture

Services 3.7 uses /user/token NOT /session/token.

/user/token is accessed via POST and it's json.

CSRF tokens need to be initialized after LOGIN to set. The code examples above use an old method.

Please update the code to reflect a working Services 3.5+ setup with jQuery.

services/session/token still works.

tyler.frankenstein's picture

GET on ?q=services/session/token still works.

Not working here

mastermindg's picture

I'm running Services 7.3.7 with Drupal 7.30 with all resources enabled on my endpoint and GET on {endpoint}/session/token does NOT work for me.

POST {endpoint}/user/token works for me fine.

My setup is out-of-the-box - no patches, etc. Why would this not be working for me?

Also, the initial CSRF token isn't re-validated after login. How are subsequent calls supposed to complete without a valid token from an authenticated session?

No endpoint needed

tyler.frankenstein's picture

?q=services/session/token is an actual path, there is no endpoint prepended to it.

?q={endpoint}/session/token [this does not exist]
?q=services/session/token [this does exist]
?q={endpoint}/user/token [this does exist]

My Bad

mastermindg's picture

The above code does work. I confirmed with PhoneGap and current jqm. Curious, though. What's the difference between the two?:

?q=services/session/token
?q={endpoint}/user/token

String vs. JSON Object

tyler.frankenstein's picture

A GET on ?q=services/session/token will return a token string:

8jY-JrOoi8RKTzJE5h6Ma3KTEEutILUL3C8KEljRxpw

A POST on ?q=endpoint/user/token.json will return a JSON object with the token bundled in it:

{"token":"Hg_dabnRffXykrUp3Zmn7hp_h3Dn_4Kq3XIYQZvUO88"}

AFAIK, that's the only difference. It appears they will not return the same token as one another. So I'd recommend using only one method when retrieving your token.