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