Я внедряю вход в Facebook в своем Android-приложении, используя класс LoginButton, предоставленный в SDK для Facebook. Все виды работ после следующих инструкций here. Единственное, что я делаю по-разному в своем приложении, это то, что я добавил кнопку входа в фрагмент как против самой активности, чтобы избежать загрязнения активности входа с большим количеством кода. Но вот проблема: Я хочу получить информацию об общедоступном профиле пользователя. И я делаю это, используя вызов Profile.getCurrentProfile()
. Вот полный код:Facebook SDK: Почему Profile.getCurrentProfile() всегда возвращает null в первый раз?
// Called in onCreate:
private void attemptLogin() {
mCallbackManager = CallbackManager.Factory.create();
FacebookCallback<LoginResult> mFacebookCallback = getFacebookCallback();
mLoginButton.registerCallback(mCallbackManager, mFacebookCallback);
}
И код mFacebookCallback это:
private FacebookCallback<LoginResult> getFacebookCallback() {
return new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if (currentProfile != null && oldProfile != null) {
mProfile = currentProfile;
// Get the new email and fire the home page of the activity, when email is available
requestUserEmail();
}
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
if (null != loginResult.getAccessToken()) {
String userId = loginResult.getAccessToken().getUserId();
Log.d(TAG, "Successfully Logged-In to Facebook");
Toast.makeText(getActivity(), "Logged-In to Facebook:" + userId, Toast.LENGTH_SHORT).show();
mProfile = Profile.getCurrentProfile();
// Now that login is successful, email can be requested
requestUserEmail();
}
}
@Override
public void onCancel() {
// App code
Log.d(TAG, "Cancelled Log-In to Facebook");
Toast.makeText(getActivity(), "Cancelled Log-In to Facebook", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d(TAG, "Error while Logging-In to Facebook");
Toast.makeText(getActivity(), "Error while Logging-In to Facebook", Toast.LENGTH_SHORT).show();
}
};
}
я и даже маркер доступа по электронной почте просто отлично. Но для первой установки приложения Profile.getCurrentProfile() всегда возвращает null.
Я также заметил, что onCurrentProfileChanged()
так и не был вызван, поскольку я ожидал, что он будет называться первым при попытке входа в систему. Я не уверен, что onCurrentProfileChanged()
код должен быть в пределах onSuccess()
. Но, кажется, людям было больше удачи, делая это, согласно вопросу here.
Что я здесь делаю неправильно? Что мне нужно сделать, чтобы убедиться, что я получаю информацию о профиле в первый раз?
это также здесь ответил: https: // StackOverflow. com/questions/29642759/profile-getcurrentprofile-returns-null-after-logging-in-fb-api-v4-0 – Aba