lucuma
lucuma

Reputation: 18339

Caching Azure Mobile Service Token for Future Requests - Facebook and Error 400

Following the various examples in the documentation authentication using the azure mobile client services for javascript works fine. I am unable to persist the returned auth token so that it can be used to see if the user is still logged in on the next request.

Starting with the initial request to login all works fine:

 client.login("facebook").then(function (results) {
   console.log(results);
   localStorageService.add('currentUser', client.currentUser);
  }, function (error) {
      console.log(error);
  });

This returns an object that looks like this in the results and client.currentUser:

{
  mobileServiceAuthenticationToken: "IHAVETRUNCATEDIT",
  userId: "Facebook:1210971539"
}

I am storing this object into localstorage (or a cookie) so that the next time a login is required I can check this token exists and pass it back to the login client service (see the token part). According to various pages the format of this at least for the facebook provider should be in the form:

{"access_token": "THEACCESSTOKEN"}

Therefor this is what is being submitted when calling the login the second (persisted) time. The token being passed in is the same one that we placed in localstorage.

 var currentUser = localStorageService.get('currentUser');

 client.login("facebook",
              { "access_token": currentUser.mobileServiceAuthenticationToken })
 .then(function (results) {
    console.log(results);
  }, function (error) {
    console.log(error);
  });

The error returned is:

Error: The Facebook Graph API access token authorization request failed with HTTP status code 400

I am not quite following how on the subsequent request (next day) to check to see if the user's token is still good.

Upvotes: 0

Views: 1074

Answers (2)

Oleg Kleiman
Oleg Kleiman

Reputation: 31

At least for Facebook, the access token returned from successful MobileServiceClient.login() request is not valid for further communication with Facebook API. Seems this is due the changes with FB Graph API done in March 2014 with their moving to version 2.2. What you can do is to perform manual login witn FB rather than use MobileServiceClient.login() and then set the obtained username and JWT to MobileServiceClient like you did:

client.currentUser = {
    userId: "Facebook:xxx",
    mobileServiceAuthenticationToken: "<your-users-JWT>"
};

Upvotes: 0

lucuma
lucuma

Reputation: 18339

It seems that the way to do this is the following:

client.currentUser = {
  userId: currentUser.userId,
  mobileServiceAuthenticationToken:   currentUser.mobileServiceAuthenticationToken
};

There is no need to login again but just set the currentUser with the saved credentials.

The following blog post I used as a reference.

Upvotes: 1

Related Questions