Wednesday, 28 September 2016

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

I have a problem with the example of the API call, the following is what I get in eclipse:

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
at adwords.axis.auth.GetRefreshToken.getOAuth2Credential(GetRefreshToken.java:86)
at adwords.axis.auth.GetRefreshToken.main(GetRefreshToken.java:122)

I already refresh token (develop token Adwords) and nothing , refresh the client id and nothing
help me!!!!!
--
--

First, make sure you follow properly the steps in the quickstart guide especially by enabling the Sheets API in your Developers Console.
Now for the Error TokenResponseException: 401 Unauthorized, based from this thread, the common causes for that error when making API calls with an access token are:
  • expired access token (most common)
  • Developer accidentally disabled the APIs (uncommon)
  • User revokes token (rare)
Sometimes, more explanation exists in the response body of a HTTP 4xx. In the Java client, for example, you should log the error, because it will assist in troubleshooting:
try {   
       // Make your Google API call
} catch (GoogleJsonResponseException e) {
      GoogleJsonError error = e.getDetails();
      // Print out the message and errors
}
You could take your existing code and make an API call here whenever you get a HTTP 4xx and log that response. This’ll return some useful information.
If the token is invalid, you can follow this steps.
  1. Remove the access token from your datastore or database.
  2. Use the refresh token to acquire a new access token (if you are using a refresh token)
  3. Try to make the API call again. If it works, you’re good! If not …
  4. Check the access token against the tokenInfo API
  5. If it’s still invalid, do a full reauth

No comments:

Post a Comment