我正在尝试在不使用任何凭据的情况下下载公共(public) Google 云端硬盘文件。我的代码如下所示:
String fileId = "id_removed";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Drive driveService = new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
}
}).setApplicationName("test app").build();
driveService.files().export(fileId, "txt")
.executeAndDownloadTo(outputStream);
String finalString = new String(outputStream.toByteArray());
System.out.println(finalString);
但这会从 google 得到 403:
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"reason" : "dailyLimitExceededUnreg",
"extendedHelp" : "https://code.google.com/apis/console"
} ],
"message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
是否可以在没有任何凭据的情况下以编程方式从 Google 下载文件?
最佳答案
注意事项:
使用您的代码至少需要使用 API key 。 当您的控制台中未激活 API key 时,您的错误通常也会出现。
解决方案:
要在没有任何 API key 或 OAuth2 凭据的情况下从 Google 云端硬盘下载文件,您可以遵循 2 种策略:
小文件:
使用通过 Drive API .get()
方法获取的 exportLinks
之一。
// This won't require any authorization
InputStream in = new URL("https://docs.google.com/feeds/download/documents/export/Export?id=####&exportFormat=docx").openStream();
Files.copy(in, Paths.get("./the_doc.docx"), StandardCopyOption.REPLACE_EXISTING);
更大的文件:
按照此 Stack Overflow 帖子中的步骤操作:https://stackoverflow.com/a/48133859/7108653
引用文献:
关于java - 无需凭据即可使用 Google 云端硬盘下载公共(public)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61218203/