java - Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized -
i tried example given here.
https://developers.google.com/sheets/quickstart/java
its giving me exception -
exception in thread "main" com.google.api.client.auth.oauth2.tokenresponseexception: 401 unauthorized @ com.google.api.client.auth.oauth2.tokenresponseexception.from(tokenresponseexception.java:105) @ com.google.api.client.auth.oauth2.tokenrequest.executeunparsed(tokenrequest.java:287) @ com.google.api.client.auth.oauth2.tokenrequest.execute(tokenrequest.java:307) @ com.google.api.client.auth.oauth2.credential.executerefreshtoken(credential.java:570) @ com.google.api.client.auth.oauth2.credential.refreshtoken(credential.java:489) @ com.google.api.client.auth.oauth2.credential.intercept(credential.java:217) @ com.google.api.client.http.httprequest.execute(httprequest.java:868) @ com.google.api.client.googleapis.services.abstractgoogleclientrequest.executeunparsed(abstractgoogleclientrequest.java:419) @ com.google.api.client.googleapis.services.abstractgoogleclientrequest.executeunparsed(abstractgoogleclientrequest.java:352) @ com.google.api.client.googleapis.services.abstractgoogleclientrequest.execute(abstractgoogleclientrequest.java:469) @ sheetsquickstart.main(sheetsquickstart.java:106)
i have given necessary permission it.
i using sheet api version v4
update -
if passing email id
in example instead of user
giving me response.
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 , 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 " + data_store_dir.getabsolutepath()); return credential; }
response -
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 further link resolve 400 - unable parse range: class data!a2:a4"
first, make sure follow steps in quickstart guide enabling sheets api in developers console.
now error tokenresponseexception: 401 unauthorized, based thread, common causes error when making api calls access token are:
expired access token (most common)
developer accidentally disabled apis (uncommon)
user revokes token (rare)
sometimes, more explanation exists in response body of http 4xx. in java client, example, should log error, because assist in troubleshooting:
try { // make google api call } catch (googlejsonresponseexception e) { googlejsonerror error = e.getdetails(); // print out message , errors }
you take existing code , make api call here whenever http 4xx , log response. this’ll return useful information.
if token invalid, can follow steps.
- remove access token datastore or database.
- use refresh token acquire new access token (if using refresh token)
- try make api call again. if works, you’re good! if not …
- check access token against tokeninfo api
- if it’s still invalid, full reauth
for more information, can check related so question.
Comments
Post a Comment