我正在尝试下载公共的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下载文件而无需任何凭据?
答案 0 :(得分:0)
使用代码至少需要使用API密钥。 如果未在控制台中激活API密钥,通常也会显示您的错误。
要从没有任何API密钥或OAuth2凭据的Google驱动器中下载文件,您可以采用以下两种策略:
使用通过Drive API exportLinks
方法获得的.get()
之一。
// 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