android - 首次登录后,Facebook Profile.getCurrentProfile() 始终返回 null

标签 android facebook-graph-api facebook-android-sdk android-facebook

当我第一次通过 Facebook 登录应用程序时,我从 Profile.getCurrentProfile(); 获取个人资料

当我退出应用程序并再次启动时,它已经登录。所以我可以直接调用 Profile.getCurrentProfile(); 返回 null。

代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_page);
    Profile profile = Profile.getCurrentProfile();
    // For the first launch the profile will be null
    displayProfileName(profile);
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("public_profile");
    callbackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    profileTracker = new ProfileTracker() {

                        @Override
                        protected void onCurrentProfileChanged(
                                Profile oldProfile, Profile currentProfile) {
                            profileTracker.stopTracking();
                            Profile.setCurrentProfile(currentProfile);
                            Profile profile = Profile.getCurrentProfile();
                            displayProfileName(profile);
                        }
                    };
                    profileTracker.startTracking();
                }

                @Override
                public void onCancel() {
                }

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

@Override
protected void onDestroy() {
    super.onDestroy();
    if (profileTracker != null) {
        profileTracker.stopTracking();
    }
}

@Override
protected void onPause() {
    super.onPause();
    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}

@Override
protected void onRestart() {
    super.onRestart();
    AppEventsLogger.activateApp(this);
}
/**
*
* Method to display the Profile Name 
*/
private void displayProfileName(Profile profile) {
    if (profile != null) {
        Toast.makeText(MainActivity.this, profile.getName(),
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(MainActivity.this, "No Profile", Toast.LENGTH_LONG)
                .show();
    }
}


@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
    super.onActivityResult(arg0, arg1, arg2);
    callbackManager.onActivityResult(arg0, arg1, arg2);
}

最佳答案

这有两种情况:

  1. 首次登录,请关注 my other answer .
  2. 对于第二次登录,您需要刷新您的AccessToken,然后获取配置文件。刷新 token 代码可以在this answer中找到但是下面的代码有它(为了简化)。

代码取自FB's dreadful documentation ).

您可以将此代码直接放入您的应用程序中,其中注释为“案例 1”,只需调用您的正常 FB 登录即可。

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();
}

关于android - 首次登录后,Facebook Profile.getCurrentProfile() 始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33252401/

相关文章:

获取 "watched"电影/电视节目列表或 "read"书籍列表的 Facebook API?

android - 获取 20 个字符的哈希键而不是 28 个用于 facebook 与 android 的集成

android - 在共享首选项中存储和检索类对象

android - Eclipse ADT ListView 和 GridView 渲染被禁用

jquery - Facebook 的“赞”按钮显示所有 Facebook 页面的赞数为 0

java - Android Facebook API 错误 : something went wrong please try again

android - 我如何使用其 Android SDK 简单地在 Facebook 墙上分享内容?

java - 从 java 应用程序发送的短信不会出现在 android 设备上

Android:在 View Canvas 上绘图不起作用

html - 无法在状态更新中更改我的网站的Facebook图标