android - Facebook Android SDK 4.0.0 没有获取个人资料信息

标签 android facebook facebook-login facebook-android-sdk

在我的应用程序中登录 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

最佳答案

对我来说,代码 Profile profile = Profile.getCurrentProfile();在安装 FB android 应用程序时工作,并且在卸载 fb 应用程序时同样抛出空指针异常,因此在那个实例中我的应用程序调用了 fb web 应用程序(它没有返回配置文件)。这可能是 FB 中的现有错误开发工具包

关于android - Facebook Android SDK 4.0.0 没有获取个人资料信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603524/

相关文章:

android - 将现有的 Android 项目导入 Eclipse 重命名项目

javascript - 使用 javascript 从 html 中提取元素

android - 如何将矢量可绘制对象的大小设置为 drawableLeft 可绘制对象?

facebook - 如何为非 App-Scoped 用户 ID 构建永久和公开的 Facebook 个人资料照片 URL?

php - Oauth 认证 facebook - cakePHP 插件

安卓磨损 : How does it work an activity life cycle?

javascript - 为什么 Facebook 点赞按钮的 JS 代码使用奇怪的 URL?

javascript - Facebook OG Meta 与 Angular 和 Node

jquery - 使用 Facebook API v.2.1 检索大个人资料图片

python - Django 2.0 - 使用 allauth 登录 Facebook - 无法加载 URL : The domain of this URL isn't included in the app's domains