这个 android 应用程序的操作流程是 -
- UserX 在 facebook 上注册
- UserX 授予 read_permissions
- UserX 授予 publish_permissions
- 他的 access_token 存储在我们的数据库中。
UserX 注销
UserY 注册 ... [遵循相同的步骤]
因为它需要多个用户登录,所以我需要 SUPPRESS_SSO(感谢How to disable Single SIgn On for facebook android app?)。
现在在 facebook API 3.0 中,Facebook 使 ReadPermissions 和 PublishPermissions 变得不同。我成功地请求了这两个权限,但它需要用户登录两次(因为 SSO 被抑制并显示 webview) - 登录 webview 再次弹出 - 用户必须输入他的电子邮件和密码。我想访问 PublishPermissions 或 ReadPermissions(以任何顺序),而无需用户重新输入密码。
java代码中的相关函数如下所示:
private static final List<String> PERMISSIONS = Arrays.asList("user_photos", "read_friendlists", "email", "publish_actions", "publish_stream");
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
handleAnnounce();
textInstructionsOrLink.setText(ACKNOWLEDGE_USER);
buttonLoginLogout.setText(R.string.logout);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
onClickLogout();
}
});
} else {
textInstructionsOrLink.setText(R.string.login);
buttonLoginLogout.setText(R.string.login);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
onClickLogin();
}
});
}
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
Session.OpenRequest openRequest = new Session.OpenRequest(this).setCallback(statusCallback);
List<String> readPermissions = Arrays.asList("user_photos", "email", "read_friendlists");
openRequest.setPermissions(readPermissions);
openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
session.openForRead(openRequest);
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private void requestPublishPermissions(Session session) {
Log.d(LOG_TAG, "Requesting Publish Permissions...");
if (session != null) {
List<String> publishPermissions = Arrays.asList("publish_actions", "publish_stream");
final int REAUTH_ACTIVITY_CODE = 100;
Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(this, publishPermissions).setRequestCode(REAUTH_ACTIVITY_CODE);
reauthRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
session.requestNewPublishPermissions(reauthRequest);
}
}
private void handleAnnounce() {
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
return;
}
List<String> permissions = session.getPermissions();
if (!permissions.containsAll(PERMISSIONS)) {
requestPublishPermissions(session);
return;
}
}