Spreadsheet Authentication in GData API

家住魔仙堡 提交于 2019-12-24 07:56:27

问题


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.



来源:https://stackoverflow.com/questions/4726209/spreadsheet-authentication-in-gdata-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!