Problem with downloading/exporting spreadsheet

330 views
Skip to first unread message

Natwalen UNK

unread,
Jan 28, 2011, 9:32:42 PM1/28/11
to google-docum...@googlegroups.com
Hi,

I tried to download a spreadsheet. This is what I did:

DocsService service = new DocsService("GDoc Service test by TW");
service.setUserCredentials("usr", "pwd");
URL documentListFeedUrl = new URL("https://docs.google.com/feeds/default/private/full/");
DocumentListFeed feed = service.getFeed(documentListFeedUrl, DocumentListFeed.class);

then I created:
String exportUrl = "https://spreadsheets.google.com/feeds/download/spreadsheets" + "/Export?key=" + docId + "&exportFormat=" + "xls";

After that I did:
MediaContent mc = new MediaContent();
mc.setUri(exportUrl);

My program returns the following:
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)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
    at com.google.gdata.client.media.MediaService.getMediaResource(MediaService.java:234)
    at com.google.gdata.client.media.MediaService.getMedia(MediaService.java:276)
    at com.google.gdata.client.media.MediaService.getMedia(MediaService.java:302)
    at dashboard.GetGoogleDoc.getEvaluationSpreadsheet(GetGoogleDoc.java:88)
    at dashboard.GetGoogleDoc.main(GetGoogleDoc.java:26)


I did some search and found somebody mentioned google Doc API service and spreadsheet API service are different. So I change the service binding to be
SpreadsheetService service = new SpreadsheetService("writely");

I am lost now. Which feedURL should I use?
1) https://docs.google.com/feeds/default/private/full/  or
2) https://spreadsheets.google.com/feeds/default/private/full/  or
3) https://docs.google.com/feeds/default/private/full/-/spreadsheet?max-results=1  ????

another problem is SpreadsheetService doesn't have a method called getMedia(MediaContent mc). how should I download the file?

anybody shed some light on me?

Thanks!

hitoshi uchida

unread,
Jan 29, 2011, 10:11:00 AM1/29/11
to Google Documents List API
Hi,
Spreadsheet API provides operations for worksheet, cell of
spreadsheet, doesn't provide a way to upload or export them. So if you
want to upload or export documents (doc, ppt, xls, pdf), you need to
use Docs API.
However, when you upload or export spreadsheets, you need to specify a
token for spreadsheet.
The token for spreadsheet can be retrieved by specifying "wise" as
service name.
So you need to handle Docs token and Spreadsheet token correctly.
> 3)https://docs.google.com/feeds/default/private/full/-/spreadsheet?max-... 

Natwalen UNK

unread,
Feb 4, 2011, 11:09:06 AM2/4/11
to google-docum...@googlegroups.com
Thanks a lot for your reply!

I changed to use Docs API:

service = new DocsService("wise");
service.setUserCredentials("my-gmail", "my-pwd");

DocumentListFeed feed = service.getFeed(documentListFeedUrl,  DocumentListFeed.class);

Then I created
String exportUrl = "https://spreadsheets.google.com/feeds/download/spreadsheets"  + "/Export?key=" + docId + "&exportFormat=" + fileExtension; -- I manually tested it and made sure I can open this URL from Chrome browser.

Then I tried to download it:


        MediaContent mc = new MediaContent();
        mc.setUri(exportUrl);
        MediaSource ms = service.getMedia(mc);
        InputStream inStream = null;
        FileOutputStream outStream = null;

        try {
            inStream = ms.getInputStream();
            outStream = new FileOutputStream(filepath);

            int c;
            while ((c = inStream.read()) != -1) {
                outStream.write(c);
            }
        } finally {
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        }

Unfortunately, I got an error message:

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>

can you or anybody else shed some light on me?

thanks!

Vic Fryzel

unread,
Feb 4, 2011, 2:46:08 PM2/4/11
to google-docum...@googlegroups.com
Since this is a spreadsheet, you have to create a SpreadsheetService, see here:

Thanks,
-Vic
Reply all
Reply to author
Forward
0 new messages