Debdeep
Debdeep

Reputation: 772

Android Graph request Facebook - Get current profile

I am writing this piece code in which during fresh login - user clicks login and then LoginManager comes out saying to continue. Just after clicking that, the user is redirected to graph request part. In this situation whenever I check whether Profile.getCurrentProfile(), it returns null. While If after returning to main screen, we re-login and graph request runs for second time,we get profile this time. Can't understand why. Thanks!

Here's the code:

 LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {

                final AccessToken accessToken = loginResult.getAccessToken();
                GraphRequest request = GraphRequest.newMeRequest(
                        accessToken,
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                    if (null == EpicLoginActivity.this) {
                                        return;
                                    }
                                    if (Profile.getCurrentProfile() != null && object != null && response != null) {
                                        profile = Profile.getCurrentProfile();
                                        email = object.optString(getString(R.string.email));
                                        gender = object.optString(getString(R.string.gender));
                                        fbid = profile.getId();
                                        fUserName = profile.getName();
                                        imageUri = profile.getProfilePictureUri(300, 300);
                                        String loginType = getString(R.string.facebook);

                                        User user = new User();
                                        user.name = fUserName;
                                        user.facebookId = fbid;
                                        user.phone = object.optString(getString(R.string.phone));
                                        user.email = email;
                                        user.loginType = loginType;
                                        user.gender = gender;

                                        postDetailsFromFb(user);
                                    }
                                } catch (Exception e) {
                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,picture");
                request.setParameters(parameters);
                request.executeAsync();
            }

Upvotes: 0

Views: 602

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

You need to put ProfileTokenTracker and AccessTokenTracker in your onSuccess() method

Full code

 private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken1) {

            }
        };
        accessTokenTracker.startTracking();

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile profile, Profile profile1) {

            }
        };
        profileTracker.startTracking();

        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
            //get data here using graph request api
            // Facebook Email address
            GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        Log.v("LoginActivity Response ", response.toString());

                        try {
                            String Name = object.getString("name");
                            String FEmail = object.getString("email");
                            Log.v("Email = ", " " + FEmail);
                            Toast.makeText(getApplicationContext(), "Name " + Name, Toast.LENGTH_LONG).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender, birthday");
        request.setParameters(parameters);
        request.executeAsync();
        }
    }

    @Override
    public void onCancel() {
        LoginManager.getInstance().logOut();

    }

    @Override
    public void onError(FacebookException e) {

    }
};

Upvotes: 1

Related Questions