2012-03-13 84 views
3

我很困惑在用戶撤銷權限後重新授權用戶的問題。當用戶撤銷應用程序的權限時,當嘗試在Facebook上發佈內容時會顯示錯誤對話框。但是,authorize()未被調用。它是我的代碼Android - 當用戶撤銷權限時,Facebook錯誤不會調用授權

   public void doAuthentication() 
      { 
        // here all your FB authentication related stuff. 

       mPrefs = getPreferences(MODE_PRIVATE); 
       String access_token = mPrefs.getString("access_token", null); 
       long expires = mPrefs.getLong("access_expires", 0); 
       if(access_token != null) { 
        facebook.setAccessToken(access_token); 
       } 
       if(expires != 0) { 
        facebook.setAccessExpires(expires); 
       } 

       /* 
       * Only call authorize if the access_token has expired. 
       */ 
       if(!facebook.isSessionValid()) { 

       facebook.authorize(Cats.this, new String[] { "publish_stream", "read_stream", "publish_checkins", "offline_access"}, 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() {} 
       }); 
      } 

      public void onActivityResult(int requestCode, int resultCode, Intent data)  { 

    super.onActivityResult(requestCode, resultCode, data); 
    facebook.authorizeCallback(requestCode, resultCode, data); 
} 
+0

什麼這裏是你的問題的問題嗎?您無法將用戶發送回認證流程? – Igy 2012-03-13 10:58:01

+0

是的。一旦用戶撤消權限並嘗試共享/發佈信息,就會出現facebook錯誤對話框,並且不會再次請求權限。 – Hend 2012-03-13 11:03:28

+0

對不起,我對Android SDK不夠熟悉;在一般情況下,應該沒有問題要求用戶重新進行身份驗證(假設他們沒有完全阻止應用程序,並且僅僅刪除了權限),但我不知道android SDK上的身份驗證流程是否足夠了解其原因不會爲你工作 – Igy 2012-03-13 11:06:09

回答

0
public void share(){ 
    if (! facebook.isSessionValid()) { 
     loginAndPostToWall(); 
    } 
    else { 
     postToWall(messageToPost); 
    } 
} 
    public void loginAndPostToWall(){ 
    facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener()); 
} 

    public void postToWall(String message){ 
    Bundle parameters = new Bundle(); 
    parameters.putString("message", message); 
    parameters.putString("description", "topic share"); 
    try { 
     facebook.request("me"); 
     String response = facebook.request("me/feed", parameters, "POST"); 
     Log.d("Tests", "got response: " + response); 
     if (response == null || response.equals("") || 
       response.equals("false")) { 
      showToast("Blank response."); 
     } 
     else { 
      showToast("Message posted to your facebook wall!"); 
     } 
     finish(); 
    } catch (Exception e) { 
     showToast("Failed to post to wall!"); 
     finish(); 
    } 
} 
    class LoginDialogListener implements DialogListener { 
    public void onComplete(Bundle values) { 
     saveCredentials(facebook); 
     if (messageToPost != null){ 
      postToWall(messageToPost); 
     } 
    } 
    public void onFacebookError(FacebookError error) { 
     showToast("Authentication with Facebook failed!"); 
     finish(); 
    } 
    public void onError(DialogError error) { 
     showToast("Authentication with Facebook failed!"); 
     finish(); 
    } 
    public void onCancel() { 
     showToast("Authentication with Facebook cancelled!"); 
     finish(); 
    } 
} 

private void showToast(String message){ 
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 
} 
private void clearCredentials() { 
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
    mAsyncRunner.logout(FacebookStatus.this, new RequestListener() { 
     @Override 
     public void onComplete(String response, Object state) { 

      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        saveCredentials(facebook); 
        Log.i("facebook session2", ""+facebook.isSessionValid()); 
        showToast("You Succesfully logged out of the app"); 
        finish(); 

       } 
      }); 

     } 

     @Override 
     public void onIOException(IOException e, Object state) {} 

     @Override 
     public void onFileNotFoundException(FileNotFoundException e, 
       Object state) {} 

     @Override 
     public void onMalformedURLException(MalformedURLException e, 
       Object state) {} 

     @Override 
     public void onFacebookError(FacebookError e, Object state) {} 
    }); 

}