java - Read data from Google Spreadsheets -
am looking read data google spreadsheets. here things tried far:
import com.google.gdata.client.spreadsheet.*; import com.google.gdata.data.spreadsheet.*; import com.google.gdata.util.*; import java.io.ioexception; import java.net.*; import java.util.*; public class myspreadsheetintegration1 { public static void main(string[] args) throws authenticationexception, malformedurlexception, ioexception, serviceexception { spreadsheetservice service = new spreadsheetservice("spreadsheetsample"); // todo: authorize service object specific user (see other sections) // define url request. should never change. url spreadsheet_feed_url = new url( "https://docs.google.com/spreadsheets/d/1jeo8zov34wvmix3rl48gkszgj0ixghx_cwdcdzyqkcg/edit#gid=0"); // make request api , spreadsheets. spreadsheetfeed feed = service.getfeed(spreadsheet_feed_url, spreadsheetfeed.class); list<spreadsheetentry> spreadsheets = feed.getentries(); // iterate through of spreadsheets returned (spreadsheetentry spreadsheet : spreadsheets) { // print title of spreadsheet screen system.out.println(spreadsheet.gettitle().getplaintext()); } } }
when run code connection time out error. feel it's authentication problem.
can please me on how achieve this?
here link spreadsheet have created test purposes. lot of code available in google none of them works far.
so need create oauth session authenticate google.
here oauth code use current project!
/** * retrieve oauth 2.0 credentials. * * @return oauth 2.0 credential instance. */ static credential getcredentials() throws ioexception { httptransport transport = new nethttptransport(); jacksonfactory jsonfactory = new jacksonfactory(); // step 1: authorize --> string authorizationurl = new googleauthorizationcoderequesturl(client_id, redirect_uri, scopes).build(); // point or redirect user authorizationurl. system.out.println("go following link in browser:"); system.out.println(authorizationurl); // read authorization code standard input stream. bufferedreader in = new bufferedreader(new inputstreamreader(system.in)); system.out.println("what authorization code?"); string code = in.readline(); // end of step 1 <-- // step 2: exchange --> googletokenresponse response = new googleauthorizationcodetokenrequest(transport, jsonfactory, client_id, client_secret, code, redirect_uri).execute(); // end of step 2 <-- authenticated = true; // build new googlecredential instance , return it. return new googlecredential.builder().setclientsecrets(client_id, client_secret) .setjsonfactory(jsonfactory).settransport(transport).build() .setaccesstoken(response.getaccesstoken()).setrefreshtoken(response.getrefreshtoken()); }
then call so
spreadsheetservice service = new spreadsheetservice("myspreadsheetintegration-v1"); service.setoauth2credentials(getcredentials());
Comments
Post a Comment