我在我的Android应用程序中使用Facebook和Parse。我学习了这教程中的Facebook登录和身份验证,在登录片段的片段和主菜单片段之间进行更改,这取决于会话状态是否是Session.StatusCallback中的OPENED。在与Parse集成之前,应用程序运行得很好。
现在我遇到了一个问题。在主菜单片段的onResume()方法中,我添加了以下代码。
final Session session = Session.getActiveSession();
if(session != null && session.isOpened()) {
Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser graphUser, Response response) {
if(session == Session.getActiveSession()) {
// Check if the session is same as usual
ParseFacebookUtils.logIn(graphUser.getId(), session.getAccessToken(),
session.getExpirationDate(), new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
// The user wasn't saved.
System.out.println("User was not saved to Parse. ");
} else {
// The user has been saved to Parse.
System.out.println("User has successfully been saved to Parse.");
if (user.isNew()) {
// This user was created during this session with Facebook Login.
System.out.println("ParseUser created.");
} else {
// This user existed before.
System.out.println("User exists in Parse. Pull their values: " + user);
}
}
}
});
}
}
});
meRequest.executeAsync();
}因此,当片段被恢复并打开Facebook会话时,Facebook用户将被添加到Parse数据库中,这样我就可以在以后使用ParseUser来处理put和get数据。但是在使用ParseFacebookUtils.logIn()时会出现这样的问题:它使Facebook会话CLOSED并调用Session.StatusCallback,从而将可见片段切换回登录片段。
我整天都在处理这个问题,但找不到解决办法。我在应用程序中添加了下面的代码,但仍然无法工作。
Parse.enableLocalDatastore(this);
Parse.initialize(this, Application_id, Client_key);
ParseFacebookUtils.initialize(getString(R.string.facebook_app_id));有办法解决这个问题吗?我读过这,但没有提供很好的解决方案。提前感谢!
发布于 2015-01-21 19:14:11
我今天也想做同样的事。我使用facebook的UiLifecycleHelper进行facebook登录,发出图形api请求,并将用户添加到Parse。你提到的例子就是这样做的--我认为这有点过时了。
ParseFacebookUtils处理会话,我们所需要做的就是调用它上的登录方法,进行图形api调用,获取用户数据(如电子邮件),并更新用户字段。
下面是一些代码:
private void parseFacebookLogin(){
ParseFacebookUtils.logIn(this, PARSE_FB_LOGIN_CODE, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d(tag, "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(tag, "User signed up and logged in through Facebook!");
Request.newMeRequest(ParseFacebookUtils.getSession(), graphUserCallback).executeAsync();
} else {
Log.d(tag, "User logged in through Facebook!");
}
}
});
}
GraphUserCallback graphUserCallback = new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null){
getUserDataFacebook(user);
Log.d(tag, response.toString());
}
}
};
private void getUserDataFacebook(GraphUser user){
//get user data here
}查看Parse在Facebook登录上的文档。
如果这对你有用的话请告诉我。
https://stackoverflow.com/questions/27744794
复制相似问题