Facebook Android SDK 4.0.0无法获取个人资料信息。

3
我在我的应用程序中登录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中检查Token时,我得到的是AccessToken token:ACCESS_TOKEN_REMOVED

5个回答

3

在我看来,当FB安卓应用程序已安装时,代码Profile profile = Profile.getCurrentProfile();能够正常运行;当FB应用程序被卸载时,则会抛出空指针异常,因此在该情况下我的应用程序调用了FB Web应用程序(它没有返回profile)。这可能是FB sdk中的一个现有bug。


我有同样的问题,有人知道任何解决方法吗? - Ege Aydın

1

不要使用loginresult.getAccessToken(),而是要使用loginresult.getAccessToken().getToken()。它返回包含访问令牌的字符串。


1

1
谢谢您的回答,但我实际上使用了这个链接 - https://developers.facebook.com/docs/facebook-login/android/v2.3 - 来编写我的代码,他们说我应该只通过getCurrentProfile方法获取个人资料,但我一直收到null。 - antrd


0

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

Profile.getProfile:是异步任务

我使用以下代码解决了这个问题:

          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() {
                 ....

最好的祝福


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接