【问题标题】:How to download text file from google drive using the url by the google drive api for android [closed]如何使用 google drive api for android 的 url 从 google drive 下载文本文件 [关闭]
【发布时间】:2015-05-07 14:27:42
【问题描述】:

我已将一个文本文件上传到我的 Google Drive 帐户并且它是公开的。我通过共享链接获得了该文件的链接。

我想要做的是使用我获得的链接使用驱动器 API 从驱动器下载该文件。然后将该文件存储在用户手机的内部存储中以备将来使用。 如何使用 API 完成此操作?

我已在 google 开发者控制台中注册了我的项目,并为我的项目创建了公共 API 访问权限。

请指导我如何获取文件。

【问题讨论】:

  • 你想把它下载到安卓设备吗?
  • 在安卓设备上是的

标签: android google-drive-api


【解决方案1】:

有 2 种不同的 API 可以从 Android 访问 Google Drive。 GDAAREST API。您需要他 REST,因为 GDAA 仅支持 FILE 范围(即只能查看 Android 应用创建的文件/文件夹)。
假设您正在询问看起来像这样的“可共享链接”:

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

下面的 URL 部分(大部分原始内容被替换为点,以保持我的内容私密):

0B1mQ................cW90ODA

是在 REST 中用于检索文件、文件夹的实际(资源)ID。看到这个sn-p:

 /*************************************************************************
   * get file contents
   * @param resId  file driveId
   * @return       file's content  / null on fail
   */
  static byte[] read(String resId) {
    byte[] buf = null;
    if (mGOOSvc != null && mConnected && resId != null) try {
      File gFl = mGOOSvc.files().get(resId).setFields("downloadUrl").execute();
      if (gFl != null){
        String strUrl = gFl.getDownloadUrl();
        InputStream is = mGOOSvc.getRequestFactory()
        .buildGetRequest(new GenericUrl(strUrl)).execute().getContent();
        buf = UT.is2Bytes(is);
      }
    } catch (Exception e) { UT.le(e); }
    return buf;
  }

GDAA/REST demo hereREST 类中脱离上下文。

您无需费心演示,您可以在playground here(页面底部)进行测试。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2015-02-06
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多