8

I'm trying to integrate facebook login on my android app. I found a lot of examples on internet, but now I have the following problem and I can't find a solution.

After facebook.authorize finish work, the oncomplete method is never called. If I use the facebook.authorize with Facebook.FORCE_DIALOG_AUTH, in this case the oncomplete method is called and I can save the access_token. I attach a snipped of my code. Can anyone help me to solve this problem?

  if(!facebook.isSessionValid()) {         
 facebook.authorize(Login.this, new String[] {"publish_stream","read_stream", "offline_access"}, //Facebook.FORCE_DIALOG_AUTH, 
        new DialogListener() {

                @Override
                public void onComplete(Bundle values) {

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();                                
                }

                @Override
                public void onFacebookError(FacebookError error) {Log.e("FB:","Facebook Error" );}

                @Override
                public void onError(DialogError e) {Log.e("FB:","Error" );}

                @Override
                public void onCancel() {}
       });
}

Thanks Simo

Simone
  • 155
  • 2
  • 7

1 Answers1

22

Make sure that you implemented onActivityResult() properly as stated in the official documentation:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}
  • Hi Alextsc, thanks for your response. the code that I have attached is for the onclicklistener method of the login button. So where I have to put the onActivityResult? Also, why with the Facebook.FORCE_DIALOG_AUTH all works as aspected? – Simone Dec 11 '11 at 15:48
  • `onActivityResult()` is a method of your Activity *(`onCreate()` and so on are also activity methods)*. You can just copy the snippet above into your activity class. I guess FORCE_DIALOG_AUTH works because facebook uses a different mechanism instead of androids activities to sent the result back to the callback (it's all webview based in this case i believe). If you want to have a definite answer to this I recommend to read the sdk code, since facebooks android sdk is open source. –  Dec 11 '11 at 20:30
  • thanks for your help alextsc. I finally solved my problem. Thanks so much – Simone Dec 12 '11 at 09:29