Facebook authentication Android


Ask by : idish November 04, 2012 00:28

So, here's my login function:

  public void facebookLogin()
{

    shareAppPreferences = new AppPreferences(getApplicationContext());
     facebook = ((GlobalVars)getApplicationContext()).facebook;

     String access_token = shareAppPreferences.getAccessToken();
     long expires = shareAppPreferences.getAccessExpires();
     Log.d("MyTag","token:" +access_token);
     if(access_token != null) {
         facebook.setAccessToken(access_token);
     }
     if(expires != 0) {
         facebook.setAccessExpires(expires);
     }
     /*
      * Only call authorize if the access_token has expired.
      */
     if(!facebook.isSessionValid()) {
         Log.d("MyTag","In Authorize");
          facebook.authorize(this, new String[] {}, new DialogListener() {
              @Override
              public void onComplete(Bundle values) {
                //  new AccessKeyTask().execute("http://37.49.226.66/allaroundme/requests.php");
                  shareAppPreferences.saveAccessToken(facebook.getAccessToken());
                  Log.d("MyTag","face token: " +facebook.getAccessToken());
                  shareAppPreferences.saveAccessExpires(facebook.getAccessExpires());

              }

              @Override
              public void onFacebookError(FacebookError error) {}

              @Override
              public void onError(DialogError e) {}

              @Override
              public void onCancel() {}
          });
     }
     Intent intent = new Intent(this,MainActivity.class);
     Log.d("MyTag", "starting activity");
     startActivity(intent);

}

I don't really understand why this code doesn't work, I logged out facebook via the facebook app before trying to use that code. the onComplete() seems not to happen and the access token is always null. but after my first login, I don't have to enter the info. Can anyone help me here?

View original question

Answer by : chRyNaNNovember 04, 2012 00:28

Here's code that worked for me:

    public void loginToFacebook(){
        facebook.authorize(CandyCount.this, new String[]{"publish_stream", "email"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {

            public void onFacebookError(FacebookError e) {
                Toast.makeText(getApplicationContext(), "onFacebookError", Toast.LENGTH_SHORT).show();
            }

            public void onError(DialogError e) {
                Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_SHORT).show();
            }

            public void onComplete(Bundle values) {

                Editor editor = sp.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();
                updateButtonStatus();

                getName();

            }

            public void onCancel() {
                // TODO Auto-generated method stub

            }

        });
    }

And in the onCreate:

    sp = getPreferences(MODE_PRIVATE);
    access_token = sp.getString("access_token", null);
    expires = sp.getLong("access_expires", 0);
    getName = sp.getString("name", null);

    if (access_token != null){
        facebook.setAccessToken(access_token);
        fbName.setText("Hello, " + getName);
        updateButtonStatus();
    }
    if (expires != 0){
        facebook.setAccessExpires(expires);

    }

In the updateButtonStatus method I change the image of the button and in the onClick of the button I either sign out or log in according to whether session is valid.

Check out this link it's pretty good in explaining the facebook sdk.

You just obviously would need to alter the code to fit your specific needs. I hope this helps!

View original answer