1

My end goal is to write a simple program that downloads one specific Google spreadsheet at the end of each week to my local hard drive. The file will be edited on a week to week basis by other people, and I will constantly need to download the updated version to pull out data and pass to a bash script that I have written. I am now doing this manually, but if I can pull down the spreadsheet in a csv format, can automate the entire process.

I am slightly confused by Google's Documentation. I have copied their example method downloadFile, but am still unclear about the exact parameters that are being passed, and the InputStream return.

If I were to add a main method to call the downloadFile method, what would be example parameters that I pass.

Then, can I use the InputPut stream it returns to save the file is csv format?

Also, is this the only method I need or do is there more to it?

downloadFile method is defined here (left out the import statements):

private static InputStream downloadFile (Drive service, File file) {
    if (file.getDownloadUrl() !=null && file.getDownloadUrl().length() > 0) {
        try {
            HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
            return resp.getContent();
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    else {
        return null;
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
user1978536
  • 95
  • 1
  • 3
  • 5
  • For the "Drive" parameter, you sure you did not mean "Driver"? I can't find any API on a Drive class. – DrinkJavaCodeJava Mar 14 '13 at 20:26
  • @DrinkJavaCodeJava Nope, it's Drive. That code was copied and pasted from Google's website. The import statement used is com.google.api.services.drive.Drive; – user1978536 Mar 14 '13 at 20:32
  • You may want to have a look at this answer. http://stackoverflow.com/questions/14133026/how-to-download-a-file-from-google-drive-using-drive-api-java?rq=1 – Dakuo Wang Jan 31 '14 at 01:07

1 Answers1

0

For the second parameter you need an instance of the File class. From what I have found out. Google's proprietory File class in it's simplest form can be instantiated with zero arguments. Like this.

File f = new File();

To build a drive object there is an example on how to do it. Scroll down to USE OAUTH 2.0 credentials. https://developers.google.com/drive/credentials

  • The File parameter is also imported from the Google API. It's not the standard Java File class. I have the libraries in my build path already, I was basically just looking to an example of how to call this method, since I can't find anyway to implement it on their website at all. – user1978536 Mar 14 '13 at 21:15