How to download text file from google drive using

2019-03-05 00:06发布

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.

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?

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.

1条回答
够拽才男人
2楼-- · 2019-03-05 00:32

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

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

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

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

taken out of context from the REST class of the GDAA/REST demo here.

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

Good Luck

查看更多
登录 后发表回答