我的FB整合很简单。对于用户的注册和登录,我只需要通过 SDK 获取 FB 令牌,然后将其交给我的服务器。我的应用程序没有进一步的图形调用等。一切都是从服务器完成的。我使用以下静态类来获取它,它工作正常。除了用户在 FB 主页上撤销对应用程序的访问权限的情况外,我仍然得到旧的访问令牌,我的服务器显然不能再使用了。
有什么办法可以强制 SDK 让我获得一个有效的令牌,并在必要时再次向用户询问权限?
public static void getAccessToken(Fragment fragment, final FacebookTokenListener listener) {
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(fragment.getActivity()).setApplicationId(
fragment.getString(R.string.facebook_app_id)).build();
Session.setActiveSession(session);
currentSession = session;
}
if (!currentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(fragment);
if (openRequest != null) {
openRequest.setPermissions(Configuration.FB_PERMISSIONS);
openRequest.setCallback(new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
Log.d(TAG, "call " + session.getAccessToken() + " , state: " + state.name() + " ,exception: "
+ (exception == null ? "null" : exception.getMessage()));
if (session.isOpened()) {
Log.d(TAG, "fb token: " + session.getAccessToken());
listener.onAccessTokenReceived(session.getAccessToken());
}
}
});
currentSession.openForRead(openRequest);
}
} else {
Log.d(TAG, "fb token: " + currentSession.getAccessToken());
listener.onAccessTokenReceived(currentSession.getAccessToken());
}
}