问题
I'm trying to download a public google drive file without using any credentials. My code looks like:
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);
But this will get a 403 from google:
{
"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."
}
Is it possible to programmatically download a file from google without having any credentials?
回答1:
Considerations:
Using your code will require at least the use of an API key. Your error usually shows up as well when the API key is not activated in your console.
Solution:
To download files from Google drive without any API key nor OAuth2 credentials, you can follow 2 strategies:
Small Files:
Using one of the exportLinks
obtained with the Drive API .get()
method.
// 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);
Bigger Files:
Follow the steps in this Stack Overflow post: https://stackoverflow.com/a/48133859/7108653
References:
Drive API Files
来源:https://stackoverflow.com/questions/61218203/google-drive-download-public-file-without-credentials