0
votes

I want to read a specific Google Drive Spreadsheet from appengine. The spreadsheet is being picked using the Google picker from a web client. I am attempting to access the Spreadsheet by using the query by title. The title gets passed to my appengine servlet and then I query using the Gdata Java Client Lib.

on appengine I have this code:

public static FeedURLFactory factory = FeedURLFactory.getDefault();
public static SpreadsheetService spreadsheetService= new SpreadsheetService("evermedcpr");

List scopes = Arrays.asList("https://spreadsheets.google.com/feeds"); 
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);

Credential creds = new Credential(BearerToken.authorizationHeaderAccessMethod());         
creds.setAccessToken(accessToken.getAccessToken());

SpreadsheetService ss = new SpreadsheetService("DBM4G-demo"); 
ss.setOAuth2Credentials(creds);

getSpreadsheet(spreadsheetName);


public SpreadsheetEntry getSpreadsheet(String spreadsheet)
      throws Exception {

      SpreadsheetQuery spreadsheetQuery 
        = new SpreadsheetQuery(new URL("https://spreadsheets.google.com/feeds/"));
      spreadsheetQuery.setTitleQuery(spreadsheet);

      SpreadsheetFeed spreadsheetFeed = spreadsheetService.query(spreadsheetQuery, 
          SpreadsheetFeed.class);
      List<SpreadsheetEntry> spreadsheets = spreadsheetFeed.getEntries();
      if (spreadsheets.isEmpty()) {
        throw new Exception("No spreadsheets with that name");
      }

      return spreadsheets.get(0);
  }

}

I always get a 404 response in my logs:

com.google.api.client.googleapis.services.AbstractGoogleClient <init>: Application name is not set. Call Builder#setApplicationName.
W 2013-07-15 14:50:31.665
[s~evermedcpr/1.368807282351251748].<stderr>: An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 OK
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "Not Found",
    "reason" : "notFound"
  } ],
  "message" : "Not Found"
}

How do I go about accessing the selected spread sheet??

2
Is the OAuth bit working? If not, use the DrEdit tutorial to get OAuth working on App Engine. After that look at CellDemo.java to learn how to use the API. (Note google apps script is simpler option, rather than the spreadsheet API) - eddyparkinson
I guess this wants someone to post a full solution. I have code to do this, but would take me too many hours to extract just the bits that are needed, and debug etc. The DrEdit bit is quite big and a few changes are needed. - eddyparkinson
@eddyparkinson yes oauth is working. I've found the issue. I was using the wrong feedurl. will update soon. - Patrick

2 Answers

2
votes

You define the SpreadsheetService twice. Once as spreadsheetService and once as ss. You only set the OAuth2 credentials to ss, but you use spreadsheetService in your getSpreadsheet method.

You should replace this:

SpreadsheetService ss = new SpreadsheetService("DBM4G-demo"); 
ss.setOAuth2Credentials(creds);

with this:

spreadsheetService.setOAuth2Credentials(creds);

You also use two different application names - BM4G-demo and evermedcpr. I am not sure which one is correct, but it is probably evermedcpr according to the server log.

Another problem is the feed URL in the SpreadsheetQuery constructor. It should be https://spreadsheets.google.com/feeds/spreadsheets (if you want all spreadsheets) or https://spreadsheets.google.com/feeds/spreadsheets/private/full (if you want only private spreadsheets). See Google Spreadsheets documentation for more details about the feed URLs.

1
votes

App Engine + Google Spreadsheet API - basic setup.

1) Google Drive OAuth

Use DrEdit tutorial to get OAuth up and running. OAuth is needed to use the Spreadsheet API.

Note you need to add refresh token to it. But that works just as you would expect.

2) Spreadsheet API

To understand how to use the Google spreadsheet API. Look at CellDemo.java (spreadsheet style access) and ListDemo.java (SQL database style access)

https://gdata-java-client.googlecode.com/svn-history/r51/trunk/java/sample/spreadsheet/cell/CellDemo.java

The code works and did not take long to get up and running. Also show how to select spreadsheets by name:

3) SpreadsheetService oauth token

SpreadsheetService spreadsheetService = null;
spreadsheetService = new SpreadsheetService("mydomain.com.au-v0.2");
spreadsheetService.setHeader("Authorization", "Bearer " + accessToken);
spreadsheetService.setConnectTimeout(0); // optional