我有下面的代码来登录 Facebook 并获取用户的电子邮件,当用户没有安装本机应用程序时,这可以正常工作,它会打开弹出窗口并让用户输入他们的 Facebook 信息。但是,当他们拥有本机应用程序时,它会请求权限,并且什么也不做。似乎我遇到了与如何禁用 facebook 本机应用程序进行用户登录以及Android Facebook 授权相同的问题 - 安装官方 Facebook 应用程序时无法登录
但是我的代码有点不同,所以我希望有人可以帮助我强制我的代码只使用弹出窗口。
我的代码
public class FbLoginActivity extends Activity {
private static List<String> permissions;
Session.StatusCallback statusCallback = new SessionStatusCallback();
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fblogin);
Button fbButton = (Button) findViewById(R.id.fbshare);
/***** FB Permissions *****/
permissions = new ArrayList<String>();
permissions.add("email");
/***** End FB Permissions *****/
fbButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Check if there is any Active Session, otherwise Open New
// Session
Session session = Session.getActiveSession();
if (session == null) {
Session.openActiveSession(FbLoginActivity.this, true,
statusCallback);
} else if (!session.isOpened()) {
session.openForRead(new Session.OpenRequest(
FbLoginActivity.this).setCallback(statusCallback)
.setPermissions(permissions));
}
}
});
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback,
savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
session.addCallback(statusCallback);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(
statusCallback).setPermissions(permissions));
}
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {
// Check if Session is Opened or not
processSessionStatus(session, state, exception);
}
}
public void processSessionStatus(Session session, SessionState state,
Exception exception) {
if (session != null && session.isOpened()) {
if (session.getPermissions().contains("email")) {
// Show Progress Dialog
dialog = new ProgressDialog(FbLoginActivity.this);
dialog.setMessage("Logging in..");
dialog.show();
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
if (user != null) {
Map<String, Object> responseMap = new HashMap<String, Object>();
GraphObject graphObject = response
.getGraphObject();
responseMap = graphObject.asMap();
Log.i("FbLogin", "Response Map KeySet - "
+ responseMap.keySet());
// TODO : Get Email
// responseMap.get("email");
String fb_id = user.getId();
String email = null;
String name = (String) responseMap
.get("name");
if (responseMap.get("email") != null) {
email = responseMap.get("email")
.toString();
Intent i = new Intent(FbLoginActivity.this, FbLogin2Activity.class);
i.putExtra("Email", email);
startActivity(i);
} else {
// Clear all session info & ask user to
// login again
Session session = Session
.getActiveSession();
if (session != null) {
session.closeAndClearTokenInformation();
}
}
}
}
});
} else {
session.requestNewReadPermissions(new Session.NewPermissionsRequest(
FbLoginActivity.this, permissions));
}
}
}
/********** Activity Methods **********/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FbLogin", "Result Code is - " + resultCode + "");
Session.getActiveSession().onActivityResult(FbLoginActivity.this,
requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Save current session
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onStart() {
// TODO Add status callback
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
protected void onStop() {
// TODO Remove callback
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
更新 所以我没有在 Facebook 开发设置中设置活动,这是有道理的,所以我应该把什么作为活动,以便 FbLoginActivity processSessionStatus 被登录触发(所以我可以获取电子邮件地址并将其传递)