我正在编写这段代码,其中在新登录期间 - 用户点击登录,然后LoginManager
出来说要继续。点击后,用户将被重定向到图形请求部分。在这种情况下,每当我检查Profile.getCurrentProfile()
时,它都返回null。如果在返回主屏幕后,我们重新登录并且第二次运行图表请求,我们这次会得到个人资料。无法理解原因。谢谢!
以下是代码:
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
final AccessToken accessToken = loginResult.getAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
if (null == EpicLoginActivity.this) {
return;
}
if (Profile.getCurrentProfile() != null && object != null && response != null) {
profile = Profile.getCurrentProfile();
email = object.optString(getString(R.string.email));
gender = object.optString(getString(R.string.gender));
fbid = profile.getId();
fUserName = profile.getName();
imageUri = profile.getProfilePictureUri(300, 300);
String loginType = getString(R.string.facebook);
User user = new User();
user.name = fUserName;
user.facebookId = fbid;
user.phone = object.optString(getString(R.string.phone));
user.email = email;
user.loginType = loginType;
user.gender = gender;
postDetailsFromFb(user);
}
} catch (Exception e) {
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture");
request.setParameters(parameters);
request.executeAsync();
}
答案 0 :(得分:1)
您需要将ProfileTokenTracker
和AccessTokenTracker
放入onSuccess()
方法
完整代码
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken1) {
}
};
accessTokenTracker.startTracking();
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
}
};
profileTracker.startTracking();
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
//get data here using graph request api
// Facebook Email address
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.v("LoginActivity Response ", response.toString());
try {
String Name = object.getString("name");
String FEmail = object.getString("email");
Log.v("Email = ", " " + FEmail);
Toast.makeText(getApplicationContext(), "Name " + Name, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
}
@Override
public void onCancel() {
LoginManager.getInstance().logOut();
}
@Override
public void onError(FacebookException e) {
}
};