簡體   English   中英

在Android上重新使用Google+的“一次性授權碼”

[英]Regeneration of “one time authorization code” for Google+ on Android

我正在根據以下內容通過Google+進行身份驗證: https//developers.google.com/+/mobile/android/sign-in

大多數這個過程似乎很好。 我遇到的問題是我們需要獲得“一次性授權代碼”,以便我們的后端服務器可以代表用戶在他們的許可下執行某些請求。 “為您的應用啟用服務器端api訪問”一節中對此進行了介紹。 但是,由於多種原因,即使授權代碼有效,我們的服務器也可能導致登錄失敗(例如,用戶在我們的服務器上沒有與Google +帳戶相對應的帳戶,在這種情況下,他們可以制作一)。

如果發生這種情況,我們可能需要他們稍后再次登錄。 但我發現,當我使用google +執行第二次登錄時,它會給我相同的授權碼 ,即使它已被我們的服務器使用過。 我已經嘗試斷開連接並重新連接到谷歌客戶端API,並調用GoogleApiClient.clearDefaultAccountAndReconnect() ,但無論我做什么,我似乎最終得到相同的授權碼。 當然,當服務器嘗試使用它時,它會被拒絕,因為它已被使用。

我想知道我在這里做錯了什么。 我有以下方法,在初始身份驗證過程中調用,然后再次從我們的服務器檢測到響應狀態為500 (表示之前的調用失敗,可能是因為代碼已被使用):

  private void dispatchGooglePlusAuthCodeAcquisition() {
    AsyncTask<Void, Void, String> authAcquisition = new AsyncTask<Void, Void, String>() {
      @Override
      protected String doInBackground(Void... params) {
        Bundle authPreferences = new Bundle();
        mUserPermissionNeededForAuthCode = false;
        authPreferences.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
                                "");
        String scopesString = Scopes.PROFILE;
        WhenIWorkApplication app = (WhenIWorkApplication)WhenIWorkApplication.getInstance();
        String serverClientID = app.getGoogleOAuthClientIDForPersonalServer();
        String scope = "oauth2:server:client_id:" + serverClientID + ":api_scope:" + scopesString;
        String code = null;
        authPreferences.putBoolean(GoogleAuthUtil.KEY_SUPPRESS_PROGRESS_SCREEN, true);

        try {
          code = GoogleAuthUtil.getToken(
            mActivity,
            Plus.AccountApi.getAccountName(mGoogleApiClient),
            scope,
            authPreferences
          );
        } catch (IOException transientEx) {
          // network or server error, the call is expected to succeed if you try again later.
          // Don't attempt to call again immediately - the request is likely to
          // fail, you'll hit quotas or back-off.
          Log.d(LOGTAG, "Encountered an IOException while trying to login to Google+."
                                   + " We'll need to try again at a later time.");
        } catch (UserRecoverableAuthException e) {
          mUserPermissionNeededForAuthCode = true;
          // Requesting an authorization code will always throw
          // UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken
          // because the user must consent to offline access to their data.  After
          // consent is granted control is returned to your activity in onActivityResult
          // and the second call to GoogleAuthUtil.getToken will succeed.
          if (!mGooglePlusPermissionActivityStarted) {
            mGooglePlusPermissionActivityStarted = true;
            mActivity.startActivityForResult(e.getIntent(), RESULT_CODE_AUTH_CODE);
          }
        } catch (GoogleAuthException authEx) {
          // Failure. The call is not expected to ever succeed so it should not be
          // retried.
          Log.e(LOGTAG, "Unable to authenticate to Google+. Call will likely never"
                                   + " succeed, so bailing.", authEx);
        }

        return code;
      }

      @Override
      protected void onPostExecute(String aResult) {
        if (aResult != null) {
          // We retrieved an authorization code successfully.
          if (mAPIAccessListener != null) {
            mAPIAccessListener.onAuthorizationCodeGranted(aResult);
          }
        } else if (!mUserPermissionNeededForAuthCode) {
          // If this is the case, then we didn't get authorization from the user, or something
          // else happened.
          if (mAPIAccessListener != null) {
            mAPIAccessListener.onAuthorizationFailed();
          }

          Log.d(LOGTAG, "Unable to login because authorization code retrieved was null");
        }
      }
    };

    authAcquisition.execute();

所以,答案比我想象的要簡單得多。 顯然, GoogleAuthUtil類上有一個clearToken()方法:

http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html#clearToken%28android.content.Context,%20java.lang.String%29

public static void clearToken(Context context,String token)

根據Context清除本地緩存中的指定標記。 請注意,上下文必須與先前調用getToken(Context,String,String)或getToken(Context,String,String,Bundle)時初始化標記的上下文相同。

參數

context令牌的上下文。
token明確的標記。

拋出

GooglePlayServicesAvailabilityException
GoogleAuthException
IOException

在嘗試重新進行身份驗證之前調用此方法會導致Google生成新的一次性授權令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM