Backendless Support
 
Answered

Get the Facebook "User Access Token" after login

Dear Support Team,

I have written an Android app that uses Backendless 3.x and my users log in with Facebook using your SDK. I now realized that I need to access the birthday Facebook attribute of the user as well. For that, the normal app secret is not enough, I need a "User Access Token". That one seems to be only provided at login. How can I retrieve it at this point? Additionally, I originally wanted to access that value in the Server Code (Custom Business Logic), but I can't get that token directly on the Server, right?

Thank you in advance

Felix D.

Best Answer
photo

The only way to retrieve additional properties from Facebook (birth date in your case) is to add it to fieldsMapping map and log in again - the user then will be requested to confirm additional data retrieval.

As to converting the value received from Facebook, it is not possible in SDK out-of-the-box, but you can try to do that using afterLogin event handler.

Leave a Comment

Comments (7)

photo
1

Hi Felix,

Firstly, have you looked into the Property Mapping section in the docs? Probably it would do exactly what you want.

In other case, please provide more details on how you perform the login and provide the code samples.

photo
1

Hello Sergey,

The problem is that I already have an existing user base and want to add their birthday now. In other words, they are already signed in for a few months. Secondly, I want to convert the Birthday into a Date object and not store it as a plain String, but is that possible with your SDK? Generally: is the User Access Token stored somewhere, on the server or the client app?

Anyways, this is how I perform the login (cleaned up up little bit, without logging and alike):

  1. public static void logIn(@NonNull Activity activity,

    @NonNull final CustomCallbacks.RetrieveCallback<CustomUser> callback,

    @NonNull CallbackManager callbackManager) {

    Map<String, String> facebookFieldsMappings = /* ... */;

    List<String> facebookPermissions = /* ... */;

    AsyncCallback<BackendlessUser> responder = new AsyncCallback<BackendlessUser>() {

    @Override

    public void handleResponse(BackendlessUser user) {

    // retrieving fresh user, since the user above is polluted with some extra attributes

    Backendless.UserService.findById(user.getUserId(), new AsyncCallback<BackendlessUser>() {

    @Override

    public void handleResponse(BackendlessUser result) {

    Backendless.UserService.setCurrentUser(result);

    CustomUser customUser = new CustomUser(result);

    callback.onSuccess(customUser);

    }

    @Override

    public void handleFault(BackendlessFault fault) {

    callback.onFailed();

    }

    });

    }

    @Override

    public void handleFault(BackendlessFault fault) {

    callback.onFailed();

    }

    };

    Backendless.UserService.loginWithFacebookSdk(activity, facebookFieldsMappings, facebookPermissions, callbackManager, responder, true);

    }

photo
1

The only way to retrieve additional properties from Facebook (birth date in your case) is to add it to fieldsMapping map and log in again - the user then will be requested to confirm additional data retrieval.

As to converting the value received from Facebook, it is not possible in SDK out-of-the-box, but you can try to do that using afterLogin event handler.

photo
1

Okay, so Logging-Out-And-In-Again is the way to go. Two simple questions:

  • Can I save the Facebook User Token this time, for possible later usage? Is that possible maybe with the field mappings?
  • Also, given that the uses logs in with the same Facebook account again, will the user be logged in to the previous account, or will a new one be crated? I don't want him/her to lose all their data.
  • Will the new mappings override existing values, or only add new ones? That could be added to the docs as I couldn't find it in there.

Thank you so far!

Felix D.

photo
1

No, the Facebook's user token is not available on the client and cannot be used directly.

The user will be relogged into his old account with new mappings - the new mappings merely override the old ones.

photo
1

Okay great, thank you! (This problem is now solved.)

Felix D.

photo
photo
1

Somehow the status of the question remained "Waiting for user's response", although I replied. Maybe this comment will change it.

Felix D.