Google云端硬盘无需证书即可下载公共文件 [英] Google drive download public file without credentials

查看:101
本文介绍了Google云端硬盘无需证书即可下载公共文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载公共的Google驱动器文件,而不使用任何凭据.我的代码如下:

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);

但这将从谷歌获得403:

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."
}

是否可以通过编程方式从Google下载文件而无需任何凭据?

Is it possible to programmatically download a file from google without having any credentials?

推荐答案

注意事项:

使用代码至少需要使用API​​密钥.如果未在控制台中激活API密钥,通常也会显示您的错误.

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.

要从Google驱动器中下载没有任何API密钥或OAuth2凭据的文件,您可以采用以下两种策略:

To download files from Google drive without any API key nor OAuth2 credentials, you can follow 2 strategies:

使用通过Drive API .get()方法获得的 exportLinks 之一.

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);

更大的文件:

按照此Stack Overflow帖子中的步骤操作: https://stackoverflow.com/a/48133859/7108653

驱动器API文件

这篇关于Google云端硬盘无需证书即可下载公共文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆