从谷歌硬盘下载文件

时间:2016-01-08 19:13:59

标签: java google-drive-api

我正在尝试从Google驱动器下载文件 我创建了谷歌凭证誓言和所有,

我完全能够获得驱动器上的文件列表 但是,当我尝试下载文件时,我收到错误500

这是我的下载代码:

  private static void downloadFile(Drive service,String fileId) {
    try {

        OutputStream outputStream = new FileOutputStream("test.csv");
        service.files().export(fileId, "text/csv")
                .executeMediaAndDownloadTo(outputStream);

    } catch (IOException e) {
        // An error occurred.
        e.printStackTrace();
    }
}

这是由此产生的回应:

    com.google.api.client.http.HttpResponseException: 500 Internal Server Error
{
  "code" : 500,
  "errors" : [ {
    "domain" : "global",
    "message" : "Internal Error",
    "reason" : "internalError"
  } ],
  "message" : "Internal Error"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeMedia(AbstractGoogleClientRequest.java:380)
    at com.google.api.services.drive.Drive$Files$Export.executeMedia(Drive.java:2464)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeMediaAsInputStream(AbstractGoogleClientRequest.java:523)
    at com.google.api.services.drive.Drive$Files$Export.executeMediaAsInputStream(Drive.java:2459)
    at com.mobile.stringExtractor.core.DriveQuickstart.downloadFile(DriveQuickstart.java:158)
    at com.mobile.stringExtractor.core.DriveQuickstart.main(DriveQuickstart.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

当我尝试下载谷歌工作表文件时,我不知道为什么它会尝试用json做些什么。

A few notes:
1. the file does exist and is not empty
2. I did authorize google drive for the said account with scopes DriveScopes.Drive
3. I do manage to download the list of files just fine

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

documentation所述,这意味着服务器暂时可用。您可以为此类错误实施exponential backoff以重新发送请求。

  

这个想法是服务器暂时不可用,并且在他们试图重新启动时不应该被淹没。

     

默认实施要求退回500和503状态代码。如果需要不同的状态代码,则子类可以覆盖。