简体   繁体   English

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

[英]Facebook Android SDK 4.0.0 not getting profile info

I have a problem with getting Profile.getCurrentProfile() after logging to Facebook in my app. 在我的应用程序中登录Facebook后获取Profile.getCurrentProfile()出现问题。 I am using Facebook Android SDK 4.0.0 . 我正在使用Facebook Android SDK 4.0.0 After logging in I would like to get user name and show it in TextView but I'm getting profile == null and i don't know why. 登录后我想得到用户名并在TextView显示它,但我得到profile == null ,我不知道为什么。 I have correct app ID and HashKey . 我有正确的应用ID和HashKey

this is my code: 这是我的代码:

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

When I check Token in Logcat i get AccessToken token:ACCESS_TOKEN_REMOVED 当我在Logcat检查Token时,我获得了AccessToken token:ACCESS_TOKEN_REMOVED

For me, the code Profile profile = Profile.getCurrentProfile(); 对我来说,代码Profile profile = Profile.getCurrentProfile(); works when the FB android application is installed, and the same throws null pointer exception, when the fb app is uninstalled and hence in that instance my app called the fb web app(which didnt return profile).This might be a existing bug in FB sdk 当安装FB android应用程序时,同样会抛出空指针异常,当fb app被卸载时,因此在那个实例中我的应用程序称为fb web app(没有返回配置文件)。这可能是FB中的现有错误SDK

Instead of loginresult.getAccessToken() , you have to use loginresult.getAccessToken().getToken() . 而不是loginresult.getAccessToken() ,您必须使用loginresult.getAccessToken().getToken() It returns the string containing the access token. 它返回包含访问令牌的字符串。

I had the same issue, this error is because: 我有同样的问题,这个错误是因为:

Profile.getProfile : Is Async task Profile.getProfile:是异步任务

I solved the issue using this code: 我使用这段代码解决了这个问题:

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

Best Regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM