8

I'm working with Titanium Studio 3.2.0 and Titanium SDK 3.2.0.GA, and deploying to Android devices with or without the Facebook app. To login to Facebook I'm using the one provided by Titanium.

I'm having the same problem described in this link. When I authorize for the first time, the log in event from Facebook is fired with no problem at all. But if I log out and then try to authorize again(at this point my app's permissions have been already accepted so in theory it should fire the login event) I get the following error on logcat:

E/FacebookModule( 1584): (main) [11631,11631] LoginDialogListener onFacebookError: Invalid access token.
E/FacebookModule( 1584): com.facebook.android.FacebookError: Invalid access token.
E/FacebookModule( 1584):    at com.facebook.android.Facebook.onSessionCallback(Facebook.java:433)
E/FacebookModule( 1584):    at com.facebook.android.Facebook.access$000(Facebook.java:97)
E/FacebookModule( 1584):    at com.facebook.android.Facebook$2.call(Facebook.java:379)
E/FacebookModule( 1584):    at com.facebook.Session$3$1.run(Session.java:1239)
E/FacebookModule( 1584):    at android.os.Handler.handleCallback(Handler.java:605)
E/FacebookModule( 1584):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/FacebookModule( 1584):    at android.os.Looper.loop(Looper.java:154)
E/FacebookModule( 1584):    at android.app.ActivityThread.main(ActivityThread.java:4624)
E/FacebookModule( 1584):    at java.lang.reflect.Method.invokeNative(Native Method)
E/FacebookModule( 1584):    at java.lang.reflect.Method.invoke(Method.java:511)
E/FacebookModule( 1584):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/FacebookModule( 1584):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/FacebookModule( 1584):    at dalvik.system.NativeStart.main(Native Method)
E/FacebookModule( 1584): (main) [6,11637] onAuthFail: Invalid access token.

I even tried to catch this error with a try/catch to handle it, but even though I placed the authorize call inside one, the code inside the catch didn't get executed. There's nothing unusual on how I do the authorization as far as I know:

$.LoginButtonHolder.addEventListener('singletap', function(e) {
        $.activityIndicator.show();
        $.LoginButtonLabel.setOpacity(0);
        try
        {
            fb.authorize();
        }
        catch(err)
        {
            // if the authorize call fails, show the error on the console and show the controls to try again
            // but these four lines of code never get executed, an unhandled exception is still thrown
            Ti.API.info('error in Facebook login');
            Ti.API.info(err);
            $.activityIndicator.hide();
            $.LoginButtonLabel.setOpacity(1);
        }
});

After some digging I realized something, the first time I try to authorize I get redirected to the Facebook app and after accepting the permissions, the login event is fired like it should (as proof of it I get an access token), when I logout the access token that was used for the session is nullified (I checked it in the logout event), then if I try to authorize again it seems the Facebook module isn't reaching out for a new access token, even though my app is already allowed, it's trying to use the nullified one, hence throwing this Invalid access token, although this is only a guess from my part and I have no idea if this is the cause.

How can I solve this problem? The solution I'm using at the moment is to remove the permissions from my Facebook user account through a Graph api call, which is not the correct way but it's the only way for me to fire the Facebook login event again.

Also I checked my hash keys and they seem to be in order since I get no invalid hash key message, the issue here is the access token, the authorize call should return an access token with a different expiration as far as I know, or did I got the wrong idea?.

According to this Facebook developers link, there are four scenarios when an access token expires, but for the log out scenario I get a different error message, the error message shown there is Error validating access token: The session is invalid because the user logged out, while the one I get is LoginDialogListener onFacebookError: Invalid access token. What is causing the error message I'm getting?

Any help will be appreciated, thanks in advance.

Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97
  • Did you still get this error if you have no facebook app present in the phone? – Sahil Mittal Mar 27 '14 at 19:45
  • No, but I need to use the facebook app login in case it's installed. I guess the main problem is with Titanium use of Activities, but other people seem to have no problem with it. – Uriel Arvizu Mar 28 '14 at 17:53
  • same problem with phonegap. i manage to come up with diffrent solution. before calling the fb.authorize, i'm checking if a specific localStorage item is exists, if not, i do the authorization and then set a new localStorage item . if yes - i handle him like he is logged in, because i know he's logged in because of the localStorage item. see localStorage - http://diveintohtml5.info/storage.html – Bonik Apr 02 '14 at 22:44
  • what specific item are you looking for? Also isn't phonegap more restricted than Titanium SDK in terms of development complexity? – Uriel Arvizu Apr 02 '14 at 23:03

1 Answers1

0

I use this to clear out any residual Facebook user data when a user logs out, Not sure how Titanium uses the Facebook SDK, but this is how I do it with stock FB SDK.

    Session.getActiveSession().closeAndClearTokenInformation();
Adam
  • 126
  • 1
  • 5