Spreadsheet Authentication in GData API

2019-07-26 16:15发布

问题:

I'm trying to edit a gdocs spreadsheet using the GData API. I have the following code:

    _service = new DocsService("attask-gdocsintegration-1.0");
    _service.setUserCredentials(username, password);

    String feedUrl = "https://docs.google.com/feeds/default/private/full";

    SpreadsheetQuery query = new SpreadsheetQuery(new URL(feedUrl));
    query.setMaxResults(1);
    query.setTitleExact(true);
    query.setTitleQuery(title);
    SpreadsheetFeed feed = _service.getFeed(query, SpreadsheetFeed.class);

    if(feed.getEntries().size() != 1) {
        throw new FileNotFoundException("'"+title+"' not a spreadsheet");
    }
    _entry = feed.getEntries().get(0);
    List<WorksheetEntry> worksheets = _entry.getWorksheets();

however, on the last line of that block, it throws an exception:

Exception in thread "main" com.google.gdata.util.AuthenticationException: Unauthorized
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600)
    ...

The username/password are correct, and seems to authenticate fine to query my documents, but soon as I try to get the worksheet it fails. Any ideas of what I'm missing here?

回答1:

after some more trial and error I found that the feed URL was incorrect. Here's what I had to change:

changed:

String feedUrl = "https://docs.google.com/feeds/default/private/full";

to:

String feedUrl = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";

and it worked.