如何从谷歌驱动器使用url由谷歌驱动的API为Android下载文本文件 [英] How to download text file from google drive using the url by the google drive api for android

查看:128
本文介绍了如何从谷歌驱动器使用url由谷歌驱动的API为Android下载文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上传文本文件到我的谷歌驱动的帐户,这是公开的。我得到的链接,该文件的共享链接。

I have uploaded a text file to my google drive account and it is public. I got the link for that file by the share link.

我想要做的就是通过我得到的链接下载使用驱动API驱动器的文件。然后存储该文​​件中的用户移动以供将来使用的内部存储设备。 如何使用API​​我做到这一点?

What I want to do is to download that file from the drive using drive API by using the link that I obtained. And then store that file in the internal storage of the users mobile for future use. How do I accomplish this using the API?

我已经注册了我在谷歌开发者控制台项目,我已经创建了我的项目一个公共的API访问。

I have registered my project in google developer console and I have created a public API access for my project.

请指引我,什么我需要做的就是文件。

Please guide me as to what I need to do to get the file.

推荐答案

有2个不同的API来访问Android的谷歌驱动器。该 GDAA 和的 REST API 。你需要他的 REST ,因为GDAA仅支持文件范围(即只能看到文件/文件夹中创建的Andr​​oid应用程序)。
假设你问的是共享链接,看起来像这样:

There are 2 different APIs to access Google Drive from Android. The GDAA and the REST API. You need he REST since GDAA supports only FILE scope (i.e. can only see files/folders the Android app created).
Assuming you are asking about the 'shareable link' that looks like this:

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

下面的URL部分(最原始的被替换点,以保持我的东西专用):

the URL portion below (most of the original is replaced by dots, to keep my stuff private):

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

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

是用于在REST检索文件,文件夹的实际(资源)的标识。看到这个片断:

is the actual (resource) ID that is used in REST to retrieve files, folders. See this snippet:

 /*************************************************************************
   * 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;
  }

在<一个断章取义href="https://github.com/seanpjanson/GDAADemo/blob/master/src/main/java/com/andyscan/gdaademo/REST.java#L198"相对=nofollow> REST 类 GDAA / REST演示这里

您不必理会该演示中,您可以在测试操场这里(在页面底部)。

You don't need bother with the demo, you can test it in the playground here (bottom of the page).

好运

这篇关于如何从谷歌驱动器使用url由谷歌驱动的API为Android下载文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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