当我尝试从 Android Facebook SDK 获取用户的电子邮件时遇到问题。我已经设置了正确的权限,并且在某些情况下它可以工作(我可以从用户那里获取所有数据),但有时在某些设备中,它不起作用。我还禁用了沙盒模式,并在 facebook 的网站上注册了我的证书。请问,谁能帮帮我?谢谢!!!!
private void signInWithFacebook() {
SessionTracker mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = mSessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
mSessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(Main.this);
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}
else{
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if(!mCurrentSession.getPermissions().contains("email")){
id = user.getId();
lastname = user.getName();
firstname = user.getFirstName();
email = user.getProperty("email").toString();
image = "http://graph.facebook.com/" + user.getId() + "/picture?type=large";
link = user.getLink();
ages = user.getBirthday();
sex = user.getProperty("gender").toString();
new CheckUser().execute();
}
else{
Toast.makeText(getApplicationContext(), "No email", Toast.LENGTH_LONG).show();
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
if (mCurrentSession.isOpened()) {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
id = user.getId();
lastname = user.getName();
firstname = user.getFirstName();
email = user.getProperty("email").toString();
image = "http://graph.facebook.com/" + user.getId() + "/picture?type=large";
link = user.getLink();
ages = user.getBirthday();
sex = user.getProperty("gender").toString();
new CheckUser().execute();
}
});
} else {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(Main.this);
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}