【问题标题】:Facebook Android SDK 4.0.0 not getting profile infoFacebook Android SDK 4.0.0 未获取个人资料信息
【发布时间】:2015-04-13 10:50:48
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    对我来说,代码 Profile profile = Profile.getCurrentProfile();在安装 FB android 应用程序时工作,并且在卸载 fb 应用程序时同样会引发空指针异常,因此在这种情况下,我的应用程序称为 fb Web 应用程序(它没有返回配置文件)。这可能是 FB 中的现有错误sdk

    【讨论】:

    • 我也有同样的问题。任何人都知道任何解决方法?
    【解决方案2】:
    【解决方案3】:

    【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

      最好的问候

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-24
        相关资源
        最近更新 更多