0

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.?

RBP
  • 481
  • 1
  • 9
  • 29
  • That error normaly comes when theres something up with the autentication. Im not a java expert, but i can take an educated guess. I would start with checking setExpiresInSeconds(min) and how you are adding the credentials to the ContactSerivce. Google around see if you can find an example of using Google Oauth2 with a service account. – Linda Lawton - DaImTo Feb 10 '14 at 14:57
  • access token valid till 1 hr, after that getting exceptions. again i need to refresh token then it is working. is there any other way ? how to keep token valid till user session..? give me some hints. – RBP Feb 11 '14 at 07:49
  • 1
    AccessTokens are short lived they are only valid for 1 hour. you need to use the RefreshToken to get a new accessToken when the first one has exipred. – Linda Lawton - DaImTo Feb 11 '14 at 07:51
  • I set Expires In Seconds 24hr time, But still it takes 1 hr.why i am unable to change timing and is this correct way to retrieve contacts using access token or there is other way also ? – RBP Feb 11 '14 at 08:47
  • Check anwser below explination was to long to add as a comment. – Linda Lawton - DaImTo Feb 11 '14 at 08:57

1 Answers1

1

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • i want to understand one thing, access Token is domain specific or user specific. means if it is domain specific then one access token will work for all users in that domain if it is user specific then i need to take each access tokens to each user. – RBP Feb 11 '14 at 09:28
  • AccessToken and RefreshToken are specific to a User's/Accounts data. In this case becouse you are using a Service account its your data. You dont have access to another users data so its not something you need to worry about. – Linda Lawton - DaImTo Feb 11 '14 at 09:40