Facebook Android SDK 4.0.0 未获取配置文件信息

Facebook Android SDK 4.0.0 not getting profile info

在我的应用程序中登录 Facebook 后,我无法获取 Profile.getCurrentProfile()。我正在使用 Facebook Android SDK 4.0.0。登录后,我想获取用户名并将其显示在 TextView 中,但我得到的是 profile == null,但我不知道为什么。我有正确的应用程序 ID 和 HashKey

这是我的代码:

public class MyFragment extends Fragment {

    CallbackManager callbackManager;
    LoginButton loginButton;
    FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Log.e("FB", String.valueOf(accessToken));
            Profile profile = Profile.getCurrentProfile();

            if (profile != null) {
                name.setText("Witam " + profile.getName());
                Log.e("FB", "w");
            }
        }

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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
        name = (TextView) view.findViewById(R.id.name);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
    }

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

当我在 Logcat 中检查令牌时,我得到 AccessToken token:ACCESS_TOKEN_REMOVED

Facebook 登录

参考这个脸书图APIurl.....

How to get user profile information from Facebook API Android

配置文件是在登录后异步获取的。如果你想跟踪它,类似于这个示例:https://github.com/facebook/facebook-android-sdk/blob/b384c0655fe96db71229bfdcb981a522f3f1e675/samples/SwitchUserSample/src/com/facebook/samples/switchuser/ProfileFragment.java#L54

对我来说,代码 Profile profile = Profile.getCurrentProfile();在安装 FB android 应用程序时工作,并且在卸载 fb 应用程序时同样抛出空指针异常,因此在那种情况下我的应用程序调用了 fb web 应用程序(没有 return 配置文件)。这可能是 FB sdk

中的现有错误

您必须使用 loginresult.getAccessToken().getToken() 而不是 loginresult.getAccessToken()。它 returns 包含访问令牌的字符串。

我遇到了同样的问题,这个错误是因为:

Profile.getProfile : Is Async task

我用这段代码解决了这个问题:

          LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    if(Profile.getCurrentProfile() == null) {
                        mProfileTracker[0] = new ProfileTracker() {
                            @Override
                            protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                                Log.v("facebook - profile", profile2.getFirstName());
                                storeLogin(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                openMainActivity(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                loadingFace.dismiss();
                                mProfileTracker[0].stopTracking();
                            }
                        };
                        mProfileTracker[0].startTracking();
                    }
                    else {
                        Profile profile = Profile.getCurrentProfile();
                        storeLogin(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        openMainActivity(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        loadingFace.dismiss();
                    }
                }
                @Override
                public void onCancel() {
                 ....

此致