Search code examples
javagoogle-docsgoogle-docs-apigoogle-sheets

Making a copy of google spreadsheet


I am trying to copy an existing spreadsheet document by following the example on the documentation page. I use ClientLogin method to authenticate. My first step is to get the template doc:

SpreadsheetService service =
    new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setUserCredentials(USERNAME, PASSWORD);

DocumentQuery query = new DocumentQuery(new URL(
    "https://spreadsheets.google.com/feeds/spreadsheets/private/full")); 
query.setTitleQuery("template"); 
query.setTitleExact(true); 

SpreadsheetFeed feed = service.getFeed(query, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();

SpreadsheetEntry template = spreadsheets.get(0);

All is fine. In template I have the document. Then I try to create a new one:

SpreadsheetEntry newDoc = new SpreadsheetEntry();
newDoc.setTitle(new PlainTextConstruct("new copy"));
newDoc.setId(template.getId());
service.insert(new URL("https://docs.google.com/feeds/default/private/full"), newDoc);

I get AuthenticationException: Token invalid. If I use https://docs.google.com/feeds/default/private/full to retrieve the document, it fails right there. So I guess docs.google.com/... requires some sort of higher privileges but how to obtain them?


Solution

  • I guess you can't create documents with ClientLogin authentication model. Use Oauth.