Facebook Profile.getCurrentProfile() is always returns null after the first login

Deadly 提交于 2019-12-06 09:04:25
Sufian

There are two cases in this:

  1. For first login, follow my other answer.
  2. For second login, you will need to refresh your AccessToken and then fetch the profile. Refreshing token code can be found in this answer but the code below has it (for simplification).

The code is taken from FB's dreadful documentation).

You can put this code straight into your app, where the comment says "case 1", just invoke your normal FB login.

private AccessTokenTracker mAccessTokenTracker;

private void loginToMyFbApp() {
    FacebookSdk.sdkInitialize(this);
    if (AccessToken.getCurrentAccessToken() != null) {
        mAccessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                mAccessTokenTracker.stopTracking();
                if(currentAccessToken == null) {
                    //(the user has revoked your permissions -
                    //by going to his settings and deleted your app)
                    //do the simple login to FaceBook
                    //case 1
                }
                else {
                    //you've got the new access token now.
                    //AccessToken.getToken() could be same for both
                    //parameters but you should only use "currentAccessToken"
                    //case 2
                    fetchProfile();
                }
            }
        };
        mAccessTokenTracker.startTracking();
        AccessToken.refreshCurrentAccessTokenAsync();
    }
    else {
        //do the simple login to FaceBook
        //case 1
    }
}

private void fetchProfile() {
    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    // this is where you should have the profile
                    Log.v("fetched info", object.toString());
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,link"); //write the fields you need
    request.setParameters(parameters);
    request.executeAsync();
}

Instead of accessing Profile.getCurrentProfile()

use access token and make graph request in onSuccess()

( It worked for me)

here snippet of code :

FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

        Log.v("profile track", (DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(loginResult.getAccessToken().getExpires())));
        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        try {

                            String name = object.getString("name");
                            String email = object.getString("email");
                            String id = object.getString("id");
                            Toast.makeText(Login.this, name + " " + " " + email + " " + id, Toast.LENGTH_SHORT).show();

                    /*write  your code  that is to be executed after successful login*/


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

    @Override
    public void onCancel() {
    }

    @Override
    public void onError(FacebookException e) {
    }
};

Call LoginManager.getInstance().logOut(); once you get the profile details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!