无法下载 CSV 格式的文件
Impossible to download a file in CSV format
我 运行 在使用 Google 驱动器 API 时遇到了一些麻烦。
我正在尝试使用 Java 应用从我自己的 Google 驱动器下载电子表格。
根据 Google ( https://developers.google.com/drive/v3/web/manage-downloads ) 的文档
可以使用 "text/csv" 参数以 CSV 格式下载电子表格。我的问题是,当我尝试以 CSV 格式下载我的文件时,我遇到了一个错误,但在其他格式下它可以正常工作。
我得到的错误是:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 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.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.executeAndDownloadTo(AbstractGoogleClientRequest.java:541)
我的代码如下:
public static Drive getDriveService() throws IOException
{
Credential credential = authorize();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws IOException
{
// Build a new authorized API client service.
Drive service = getDriveService();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
List<File> files = result.getFiles();
if (files == null || files.size() == 0)
{
System.out.println("No files found.");
}
else
{
for (File file : files)
{
if (file.getName().equals(Settings.DRIVE_FILE_NAME))
{
System.out.printf("%s (%s)\n", file.getName(), file.getId());
String fileId = file.getId();
OutputStream outputStream = new ByteArrayOutputStream();
service.files().export(fileId, "text/csv").executeAndDownloadTo(outputStream);
System.out.println(outputStream);
}
}
}
}
我哪里做错了?
谢谢!
根据官方 Google 文档,当您收到“500:后端错误”时,这是在处理请求时发生意外错误。
建议的操作是使用“指数退避”。 指数退避 是网络应用程序的标准错误处理策略,在该策略中,客户端会在不断增加的时间内定期重试失败的请求。如果大量请求或繁重的网络流量导致服务器出现 return 错误,指数退避可能是处理这些错误的好策略。
指数退避提高了带宽使用效率,减少了获得成功响应所需的请求数。
更多详情,请按照link:https://developers.google.com/drive/v3/web/handle-errors#500_backend_error强调文字
我 运行 在使用 Google 驱动器 API 时遇到了一些麻烦。 我正在尝试使用 Java 应用从我自己的 Google 驱动器下载电子表格。
根据 Google ( https://developers.google.com/drive/v3/web/manage-downloads ) 的文档 可以使用 "text/csv" 参数以 CSV 格式下载电子表格。我的问题是,当我尝试以 CSV 格式下载我的文件时,我遇到了一个错误,但在其他格式下它可以正常工作。
我得到的错误是:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 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.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.executeAndDownloadTo(AbstractGoogleClientRequest.java:541)
我的代码如下:
public static Drive getDriveService() throws IOException
{
Credential credential = authorize();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws IOException
{
// Build a new authorized API client service.
Drive service = getDriveService();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
List<File> files = result.getFiles();
if (files == null || files.size() == 0)
{
System.out.println("No files found.");
}
else
{
for (File file : files)
{
if (file.getName().equals(Settings.DRIVE_FILE_NAME))
{
System.out.printf("%s (%s)\n", file.getName(), file.getId());
String fileId = file.getId();
OutputStream outputStream = new ByteArrayOutputStream();
service.files().export(fileId, "text/csv").executeAndDownloadTo(outputStream);
System.out.println(outputStream);
}
}
}
}
我哪里做错了?
谢谢!
根据官方 Google 文档,当您收到“500:后端错误”时,这是在处理请求时发生意外错误。
建议的操作是使用“指数退避”。 指数退避 是网络应用程序的标准错误处理策略,在该策略中,客户端会在不断增加的时间内定期重试失败的请求。如果大量请求或繁重的网络流量导致服务器出现 return 错误,指数退避可能是处理这些错误的好策略。
指数退避提高了带宽使用效率,减少了获得成功响应所需的请求数。
更多详情,请按照link:https://developers.google.com/drive/v3/web/handle-errors#500_backend_error强调文字