2012-05-14 54 views
0

當用戶通過Facebook撤消應用權限時,此代碼應該要求用戶重新授權,但不是。任何人都可以看到錯誤? 「臉譜」是一個Facebook對象。Android: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

這就是Facebook的SDK文檔說:

您可以通過檢查的onComplete方法的響應 參數檢測的訪問令牌的錯誤。在這種情況下,您將再次需要 調用facebook.authorize()重新驗證用戶身份並生成新的訪問令牌 。

您需要檢查您的Facebook電話的響應以檢測此類sitautuons。例如:

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; 
    }  
}