当用户通过facebook撤销app权限时,此代码应该要求用户重新授权,但事实并非如此。任何人都可以看到错误吗? “facebook”是一个Facebook对象。
public void facebookSetup() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null); //load an access token from sharedPreferences
long expires = mPrefs.getLong("access_expires", 0); //do the same for the token expiration time
if (access_token != null) { //if the access token exists in sharedPreferences, we'll use it
facebook.setAccessToken(access_token);
}
if (expires != 0) { //if the expiration time exists in shared preferences, use it
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "publish_stream", "user_photos", "publish_actions"}, new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
}
答案 0 :(得分:0)
这是Facebook SDK文档所说的:
您可以通过检查响应来检测访问令牌错误 onComplete方法的参数。在这种情况下,您将再次需要 调用facebook.authorize()来重新验证用户并生成 一个新的访问令牌。
您需要检查您的Facebook通话的响应,以检测此类sitautuons。 E.g:
JSONObject responseJSON = new JSONObject(response);
JSONObject errorJSON = responseJSON.optJSONObject("error");
if (errorJSON!=null) {
String error =errorJSON.optString("type");
if (error!=null && error.equals("OAuthException")) {
facebook.authorize(activity, FACEBOOK_PERMISSIONS, facebookDialogListener);
return;
}
}