2015-12-27 42 views
0

嗨,我想明確覆蓋客戶經理現有的AuthToken。我試圖用這個片段Android AccountManager需要覆蓋AuthToken

Log.d(TAG, "finishRenewal"); 
AccountManager manager = AccountManager.get(context); 
Account[] accounts = manager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE); 
for(Account ac : accounts){ 
    manager.setAuthToken(ac, AccountGeneral.ACCOUNT_TYPE, authToken); 
} 

但我似乎無法獲取的authToken。它總是給出以前的AuthToken。

BTW:我對這個調用

AccountManagerFuture<Bundle> future = mAccountManager 
    .getAuthTokenByFeatures(accountType, authTokenType, null, act, null, null, 
     new AccountManagerCallback<Bundle>() { 
      @Override 
      public void run(AccountManagerFuture<Bundle> future) { 
      Bundle bnd = null; 
      try { 
       bnd = future.getResult(); 
       final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 
       final String userName = bnd.getString(AccountManager.KEY_ACCOUNT_NAME); 
       Log.d(TAG, "GetTokenForAccount Bundle is " + bnd); 
       if (listener != null) { 
       listener.onAccountAcquired(userName, authtoken); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       if (listener != null) { 
       listener.onFail(); 
       } 
      } 
      } 
     } 
     , null); 
+0

最初如何設置授權令牌?你如何檢索身份驗證令牌?還要確保你的代碼片段是正確的。 Android的AccountManager沒有一個不帶參數的'invalidateAuthToken()'方法。 – Marten

+0

對不起,我必須粘貼錯誤的代碼片段,不管我編輯它以反映正確的片段。我正在通過此調用檢索令牌:AccountManagerFuture future = mAccountManager .getAuthTokenByFeatures(params); – Lester

回答

1

我的猜測檢索令牌是,authTokenType在第二代碼片段的值不從第一個片段等於AccountGeneral.ACCOUNT_TYPE

當您設置授權令牌時,您的代碼將通過AccountGeneral.ACCOUNT_TYPE作爲AuthTokenType。確保在設置authtoken時使用正確的AuthTokenType,而不是帳戶類型。

AuthTokenType通常與賬戶類型不同,因爲它們識別不同的事物。

+0

謝謝,我可能錯過了那一個。我應該給AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS作爲setAuthToken方法的參數。 – Lester