Вопрос

I am getting Token Response Exception after 1-2 min continuously. After 2-3 min contacts coming and then after 2-3 min again token exception is coming. Below is the Exception

com.google.api.client.auth.oauth2.TokenResponseException: 403 OK

<p class="large"><b>403.</b> 
<ins>That's an error.</ins></p><p class="large">You are not authorised to perform this request.  <ins>That's all we know.</ins>
</p>

I am retriving contacts of user , Below is my code,

 ContactsService contactService = new ContactsService("appName");
 contactService.setOAuth2Credentials(getCredentials());

Below is getCredentials() method.

public  GoogleCredential getCredentials()  {
    GoogleCredential credential = null;
    try{

         Collection<String> SCOPES = new ArrayList<String>();
         SCOPES.add("https://www.googleapis.com/auth/userinfo.profile");
         SCOPES.add("https://www.google.com/m8/feeds");
         HttpTransport httpTransport = new NetHttpTransport();
         JacksonFactory jsonFactory = new JacksonFactory();
         credential = new GoogleCredential.Builder().setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountUser(adminEmailAddress)
                .setServiceAccountPrivateKeyFromP12File(new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
                .build().setExpiresInSeconds(min);

         credential.refreshToken();

    } catch(Exception e){
        e.printStackTrace();
    }
    return credential;
}

can anyone tell me how to keep token valid for max time or how to deal with above problem.?

Это было полезно?

Решение

You need to understand how Oauth2 works I think you should read Using OAuth 2.0 to Access Google APIs

  1. Refresh the access token, if necessary.

Access tokens have limited lifetimes. If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

Note: Save refresh tokens in secure long-term storage and continue to use them as long as they remain valid. Limits apply to the number of refresh tokens that are issued per client-user combination, and per user across all clients, and these limits are different. If your application requests enough refresh tokens to go over one of the limits, older refresh tokens stop working.

As stated in the doucmentation access tokens work for a limited amount of time. That being 1 hour you can't extend that. But you have the refreshToken you need in order to get a new AccessToken. RefreshTokens dont expire unless the user revokes your access. But in your case this wont happen becouse you are using a service account. So you can just rerun your code and get a new AccessToken

You have two options:

  1. Check the time that is returned if your access token is about to expire then rerun the code and get a new one.
  2. Wait until you get the error message then request a new access token.

The first option is best becouse google logs the number of errors you get from the API no reason to run something thats going to error on you. I normally request a new AccessToken 5 minutes before my old one is due to expire.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top