“Login Required” 401 Unauthorized message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0

后端 未结 6 639
时光取名叫无心
时光取名叫无心 2020-12-29 06:35

First, let me explain what I am trying to do, as this is a two part question.

I am building a JAX-RS service that internally authenticates with a Google account via

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 06:59

    As for the 401, it seems some oddity with Google's API. I find the first time I run my app in a while, and then at seemingly random intervals hours spread apart, I always get a 401. On the second or third round it actually logs in.

    The behavior is even accounted for in Google's Task API sample code here.

    Notice this code:

    void onRequestCompleted() {
    received401 = false;
    }
    
    void handleGoogleException(IOException e) {
    if (e instanceof GoogleJsonResponseException) {
      GoogleJsonResponseException exception = (GoogleJsonResponseException) e;
      if (exception.getStatusCode() == 401 && !received401) {
        received401 = true;
        accountManager.invalidateAuthToken(credential.getAccessToken());
        credential.setAccessToken(null);
        SharedPreferences.Editor editor2 = settings.edit();
        editor2.remove(PREF_AUTH_TOKEN);
        editor2.commit();
        gotAccount();
        return;
      }
    }
    Log.e(TAG, e.getMessage(), e);
    

    }

    In Google's own code sample they have made provision for a first time 401. It might be some special security issue that needs to be handled in code.

提交回复
热议问题