1

I am integrating Facebook login in my android app and accessing user info using Graph API.

I am getting an unusual behaviour, when i launch the app in which i am not logged in with Facebook and then try to login in with Facebook then it allows me to successfully login but doesn't allow to get the user info(that is NullPointerException on JSON object) and it crashes.

On the other hand if i again relaunch the app in which i am already logged in(successfully logged in before app crashing) and then press logout button and then login button again. Now strange thing happens and I can access user info after successful login. Please help me to access user info in the first case also.

Here is my code :

public class Login extends Activity implements View.OnClickListener{

private LoginButton facebookLogin;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private AccessToken accessToken;
private Profile facebookProfile;
private ProfileTracker profileTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    setContentView(R.layout.activity_login);
    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            // Set the access token using
            // currentAccessToken when it's loaded or set.
        }
    };

    // If the access token is available already assign it.
    accessToken = AccessToken.getCurrentAccessToken();

    facebookProfile = Profile.getCurrentProfile();

    /*profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(
                Profile oldProfile,
                Profile currentProfile) {
            // App code
        }
    };*/

    facebookLogin = (LoginButton)findViewById(R.id.button_facebookLogin_Login);
    facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
    facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
            GraphRequest request = GraphRequest.newMeRequest(accessToken,
                    new GraphRequest.GraphJSONObjectCallback(){

                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {

                            Log.d("TOP","Object = "+object.toString());

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender");
            request.setParameters(parameters);
            request.executeAsync();
            // App code
        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });

}

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

@Override
public void onDestroy() {
    super.onDestroy();
    //accessTokenTracker.stopTracking();
    //profileTracker.stopTracking();
}

}
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Algor7
  • 149
  • 1
  • 14

1 Answers1

1

Can you try to create Graph request like me : Getting email from Facebook - FB android SDK

When your Callback finished , use AccessToken like : loginResult.getAccessToken()

facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback(){

                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {

                        Log.d("TOP","Object = "+object.toString());

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender");
        request.setParameters(parameters);
        request.executeAsync();
        // App code
    }

If you want to see example Json format try Graph Api in : Graph Api Explorer

Just try this way and come with result please !

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • Thankx @Yasin it is working now. But could you please tell the reason it worked. – Algor7 Jun 15 '16 at 14:44
  • @Algor7 you need valid AccessToken in your GraphRequest. When you click your Facebook button it will ask user for authenticate and generates authenticated loginresult. And then you can use authenticate token for your parameters, graph request. But in your accessToken = AccessToken.getCurrentAccessToken() method, what is CurrentAccessToken ? , is CurrentAccessToken valid for your variables (picture, email , profile etc.) – Yasin Kaçmaz Jun 15 '16 at 14:58
  • As per my knowledge, accessToken is the current access token facebook provide me just after successful login. But i guess it is placed in the wrong place or not updating that's why i was not able to access user info after first login because i had already initialized it way before getting the actual token. And second time when i was loggin in it was working with the previous token. This my guess but i am not sure. – Algor7 Jun 15 '16 at 15:55