java - Google Spreadsheet with a service account -


i want access spreadsheet on google drive using de java google spreadsheet api. in past used client login method login method deprecated now. i'm trying use service account getting authenticated without interaction user because tool running in background needs automated. can't working. these code now.

    list<string> scopes_array = arrays.aslist(             "https://www.googleapis.com/auth/drive.file",             "https://www.googleapis.com/auth/userinfo.email",             "https://www.googleapis.com/auth/userinfo.profile",             "https://docs.google.com/feeds",             "https://spreadsheets.google.com/feeds");     credential = new googlecredential.builder()                 .settransport(transport)                 .setjsonfactory(jsonfactory)                 .setserviceaccountid(                         "xxxxxx@developer.gserviceaccount.com")                 .setserviceaccountscopes(scopes_array)                 .setserviceaccountprivatekeyfromp12file(p12)                 .setserviceaccountuser("me@gmail.com")                 .build();     service = new spreadsheetservice("monitorv3");     service.setoauth2credentials(credential);      spreadsheet_feed_url = new url("https://spreadsheets.google.com/feeds/spreadsheets/private/full");      worksheetfeed = service.getfeed(spreadsheet.getworksheetfeedurl(),                     worksheetfeed.class); 

but i'm getting nullpointerexception when executing last line. goes wrong refresh token. there seeing i'm doing wrong?enter image description here

i made standalone simple application , following detailded error:

exception in thread "main" com.google.gdata.util.authenticationexception: failed refresh access token: 401 unauthorized @ com.google.gdata.client.googleauthtokenfactory$oauth2token.refreshtoken(googleauthtokenfactory.java:260) @ com.google.gdata.client.googleauthtokenfactory.handlesessionexpiredexception(googleauthtokenfactory.java:702) @ com.google.gdata.client.googleservice.handlesessionexpiredexception(googleservice.java:738) @ com.google.gdata.client.googleservice.getfeed(googleservice.java:649) @ com.google.gdata.client.service.getfeed(service.java:1017) @ javaapplication20.printdocuments(javaapplication20.java:50) @ main.main(main.java:35) 

i fixed using following code:

credential = new googlecredential.builder()             .settransport(transport)             .setjsonfactory(jsonfactory)             .setserviceaccountid("618982716759-9ele96d95b7gqar6tn2ofa7jrjrudlol@developer.gserviceaccount.com")             .setserviceaccountprivatekeyfromp12file(p12)             .setserviceaccountscopes(scopes_array).build();             credential.refreshtoken(); 

so without .setserviceaccountuser("me@gmail.com"). it's working without errors.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -