当我尝试通过 facebook 对话框登录后请求发布权限(未安装 fb 应用程序)时,我被要求再次登录,之后一切正常运行。有没有办法避免这个问题?这是我的代码:
private void login() {
Session.Builder builder = new Session.Builder(activity);
builder.setApplicationId(activity.getString(R.string.app_id));
builder.setTokenCachingStrategy(new SharedPreferencesTokenCachingStrategy(activity));
Session session = builder.build();
Session.setActiveSession(session);
Session.OpenRequest request = new Session.OpenRequest(activity).setCallback(new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (exception!=null) {
Toast.makeText(activity, exception.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
if (session.isOpened())
checkAndRequestPermissions();
}
});
session.openForRead(request);
}
private void checkAndRequestPermissions() {
Session session = Session.getActiveSession();
List<String> permissionsList = new ArrayList<String>();
if (!session.getPermissions().contains(USER_PHOTOS_PERMISSION))
permissionsList.add(USER_PHOTOS_PERMISSION);
if (!session.getPermissions().contains(PUBLISH_ACTIONS_PERMISSION))
permissionsList.add(PUBLISH_ACTIONS_PERMISSION);
if (permissionsList.size()>0) {
Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(activity, permissionsList);
session.requestNewPublishPermissions(request);
}
else {
publishOnFacebook(filesToPublish);
}
}