【问题标题】:Does Google Drive API Allow us to download public file without authentication?Google Drive API 是否允许我们无需身份验证即可下载公共文件?
【发布时间】:2017-10-31 18:07:08
【问题描述】:

我想知道我可以使用 API 从 Google Drive 下载公共文件而无需身份验证吗?可能我可以,因为如果我在没有身份验证的情况下看到它,任何文件都可以下载。但是 Google Drive API Docs 说如下;

Every request your application sends to the Drive API must include an authorization token.

例如,我可以显示以下公共文件并且我正在尝试获取它。也许,我什至不需要使用 Google Drive API。顺便说一句,我正在使用 JAVA。

https://drive.google.com/file/d/0B1dXCaVmaqzROHNwdC1SQmdxejQ/view?usp=sharing

我能做什么,有什么建议吗?

【问题讨论】:

    标签: java api file authentication google-drive-api


    【解决方案1】:

    您无需身份验证即可读取公共文件。文件下载的 URL 很简单:https://www.googleapis.com/drive/v3/files/<FILE_ID>?key=<YOUR_API_KEY>&alt=media

    【讨论】:

      【解决方案2】:

      https://www.googleapis.com/drive/v3/files/<FILE_ID>?key=<YOUR_API_KEY>&alt=media 给了我 CORS 错误,所以这就是我所做的:

      1. 在没有alt=media的情况下调用https://www.googleapis.com/drive/v3/files/<FILE_ID>?key=<YOUR_API_KEY>

      2. 在响应时读取属性downloadUrlmimeType

      3. responseType: arrayBuffer调用downloadUrl

      4. 使用blobarrayBuffer 转换为可以下载的文件

      以下关于 Angular 的代码可以完成这项工作:

      async downloadZip(): Promise<void> {
        try {
          const meta = await this.http.get(
            'https://www.googleapis.com/drive/v2/files/<FILE_ID>?key=<API_KEY>'
            ).toPromise() as { downloadUrl: string, mimeType: string };
          const arrBuffer = await this.http.get(meta.downloadUrl, { responseType: 'arraybuffer'}).toPromise();
          this.saveArrBuffer('testfile', meta.mimeType, arrBuffer);
      
        } catch (e) {
          console.log(e);
        }
      }
      
      saveArrBuffer(fileName: string, type: string, arr: ArrayBuffer): void {
        const blob = new Blob([arr], { type });
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;
        link.click();
      }
      

      我为此苦苦挣扎,我很乐意发布。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        相关资源
        最近更新 更多