0

当用户通过 facebook 撤销应用程序权限时,此代码应要求用户重新授权,但事实并非如此。有人能看出什么不对吗?“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() {}
        });
    } 
}
4

1 回答 1

0

这就是 Facebook SDK 文档所说的:

您可以通过检查 onComplete 方法的响应参数来检测访问令牌错误。在这种情况下,您将再次需要调用 facebook.authorize() 来重新验证用户并生成新的访问令牌。

您需要检查您的 Facebook 呼叫的响应以检测此类情况。例如:

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; 
    }       
}       
于 2012-10-23T15:54:53.113 回答