2012-11-09 54 views
0

我正在關注this tutorial。我儘可能地從savedPreferences中檢索訪問令牌,然後創建一個註銷按鈕。問題是,似乎我access_token永遠是正確的,無論我從我的應用程序,甚至從Facebook應用程序註銷。Facebook Android教程註銷

這是我的代碼:

public class MainActivity extends Activity { 
    private String TAG = MainActivity.class.getName();  

    Facebook mFacebook = new Facebook("whatever"); 
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

    private SharedPreferences mPrefs; 

    //UI elements 
    private ImageButton fbLoginButton; 
    private Button getInfoButton; 
    private Button fbLogoutButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mPrefs = getPreferences(MODE_PRIVATE); 
     String access_token = mPrefs.getString("access_token", null); 
     long expires = mPrefs.getLong("access_expires", 0); 

     if(access_token != null) { 
      Log.d(TAG, "Token found. Setting token " + access_token); 
      mFacebook.setAccessToken(access_token); 
     } 

     if(expires != 0) { 
      Log.d(TAG, "Setting expire " + expires); 
      mFacebook.setAccessExpires(expires); 
     } 


     if(!mFacebook.isSessionValid()) { 
      mFacebook.authorize(this, new String[] {"email", "read_friendlists" }, new DialogListener() { 
       @Override 
       public void onComplete(Bundle values) { 
        SharedPreferences.Editor editor = mPrefs.edit(); 
        editor.putString("access_token", mFacebook.getAccessToken()); 
        editor.putLong("access_expires", mFacebook.getAccessExpires()); 
        editor.commit(); 
        Log.d(TAG, "Logging you in"); 
       } 

       @Override 
       public void onFacebookError(FacebookError error) { 
        Log.d(TAG, "Facebook error: " + error.getMessage()); 

       } 

       @Override 
       public void onError(DialogError e) { 
        Log.d(TAG, "Error: " + e.getMessage()); 
       } 

       @Override 
       public void onCancel() { 
        Log.d(TAG, "User Canceled"); 
       } 
      }); 
     } 
     else{ 
      Toast.makeText(this, "Already logged in", Toast.LENGTH_LONG).show(); 
     } 


     fbLoginButton = (ImageButton) findViewById(R.id.buttonFBLogin); 
     getInfoButton = (Button) findViewById(R.id.buttonGetInfo); 
     fbLogoutButton = (Button) findViewById(R.id.buttonLogout); 

     fbLogoutButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       doLogout(); 
      } 
     }); 
    } 

    protected void doLogout() { 
     mAsyncRunner.logout(this, new RequestListener() { 
       @Override 
       public void onComplete(String response, Object state) { 
        Log.d(TAG, "Logging you out"); 
       } 

       @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) {} 
      }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     mFacebook.authorizeCallback(requestCode, resultCode, data); 
    } 
} 

我總是得到Already logged in Toast.Am我做錯了什麼?

PS。在從sharedpreferences檢索access_token之前,該應用程序似乎工作正常,所以它不是我的API密鑰或調試密鑰的問題。

+0

嘗試將一些日誌消息放入'mAsyncRunner.logout'內的不同函數中,以查看您是否遇到某種錯誤 –

+0

@Kelmer您是否解決了此問題? Iam也面臨同樣的問題Iam沒有得到任何解決方案。 – Mano

回答

0

嘗試mFacebook.logout並檢查響應值。如果註銷成功,它將是「真」。適用於我。

不要忘記單點登錄功能。如果你仍然登錄Facebook應用程序,它不會真的註銷你。

+0

不,但它會撤銷令牌,因此它會再次被要求,不是嗎?無論如何,當從Facebook應用程序退出時,它仍然給我「已經登錄」的消息 –

+0

也許這是Toast.makeText(這是「已經登錄」,Toast.LENGTH_LONG).show();給你這個消息?當你從Facebook登出時,它會清除所有的令牌,所以mFacebook.isSessionValid()在此之後返回false,因爲你的會話不再有效。 –

+0

確切地說,因爲它應該返回false,所以不應該出現敬酒,因爲它在if語句的另一條路徑中。 –