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

I tried the example given here.

https://developers.google.com/sheets/quickstart/java

This gives me this exception -

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.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570) at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) at SheetsQuickstart.main(SheetsQuickstart.java:106) 

I gave him all the necessary permissions.

I am using api version v4

Update -

If I pass the email id in the example instead of user , then it gives me this answer.

Changes -

 public static Credential authorize() throws IOException { // Load client secrets. InputStream in = SheetsQuickstart.class.getResourceAsStream("/client_secret.json"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(DATA_STORE_FACTORY) .setAccessType("offline") .build(); //Changed Part. Credential credential = new AuthorizationCodeInstalledApp( flow, new LocalServerReceiver()).authorize(" test@gmail.com "); System.out.println( "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); return credential; } 

The answer is

 Name, Major Alexandra, English Andrew, Math Anna, English Becky, Art Benjamin, English Carl, Art Carrie, English Dorothy, Math Dylan, Math Edward, English Ellen, Physics Fiona, Art John, Physics Jonathan, Math Joseph, English Josephine, Math Karen, English Kevin, Physics Lisa, Art Mary, Physics Maureen, Physics Nick, Art Olivia, Physics Pamela, Math Patrick, Art Robert, English Sean, Physics Stacy, Math Thomas, Art Will, Math 

I got additional help from this link to allow 400 - Unable to parse range: Class Data!A2:A4"

400 Invalid request resolution

+5
source share
1 answer

First, make sure you follow the steps in the quick start guide correctly, especially by enabling the sheet API in the developer console.

Now for Error TokenResponseException: 401 Unauthorized , based on this thread , common causes of this error when making API calls with an access token:

  • Expired access token (most common)

  • Developer accidentally disables API (rarely)

  • User revokes token (rarely)

Sometimes more explanation appears in the body of an HTTP 4xx response. For example, in the Java client, you should register an error, as it will help in troubleshooting:

 try { // Make your Google API call } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); // Print out the message and errors } 

You can take your existing code and call the API call here whenever you receive HTTP 4xx and register this response. This will return some useful information.

If the token is invalid, you can follow these steps.

  • Remove an access token from a data warehouse or database.
  • Use the update token to get a new access token (if you use the update token)
  • Retry the API call. If this works, you are good! If not...
  • Check access token against tokenInfo API
  • If still not valid, run the full reauth command

For more information, you can check out this related SO question .

+3
source

All Articles