Google Drive API v3 : there isn't any way to get a download url for a google document?

十年热恋 提交于 2019-12-06 07:54:59

问题


The Google Drive API v2 to v3 migration guide says:

The exportLinks field has been removed from files. To export Google Documents, use the files.export method instead.

I don't want to export (download) the file right away. "files.export" will actually download the file. I want a link to download the file, later. This was possible in v2 by means of the exportLinks.

How can I in v3 accomplish the same? If it is not possible, why was this useful feature removed?

Besides, (similar problem to above) downloadUrl was also removed, and the suggested alternative ("files.get with ?alt=media") downloads the file instead of providing a download link. This means there is no way in v3 to get a public short lived URL for a file?

EDIT:

there is no way in v3 to get a public short lived URL for a file?

For regular files, apparently yes.

This seems to work fine (a public short lived link to the file with its right name and contents): https://www.googleapis.com/drive/v3/files/ID?alt=media&access_token=TOKEN

For google apps files, no (not even private, as v2 exportLinks used to be).

https://www.googleapis.com/drive/v3/files/ID/exportmimeType=TYPEv&access_token=TOKEN

Similar to regular files, this URL is a short lived link to the file contents, but lacking of its right name.

BTW, I see the API is not behaving consistently: /drive/v3/files/FILEID delivers the right file name, but /drive/v3/files/FILEID/export does not.

I think the API itself should be setting the right Content-Disposition, as it is apparently doing when issuing a /drive/v3/files/FILEID call.

This file naming problem invalidates the workaround to the lack of ExportLinks in v3.

The v2 ExportLinks allowed me to link a file (which is not the same as getting its content right away). Anyone logged in and with the proper permissions was able to access it, and the link didn't needed any access_token, and it wasn't short lived. It was good and useful.

Building a link with a raw API call like /drive/v3/files/FILEID/export (with mandatory access_token) would be an close enough workaround (it is temporary and public, not the same as it was, anyway). However, the naming problem invalidates it.

In v2, regular files have a WebContentLink and google apps files have exportLinks. In v3 exportLinks are gone, and I don't see any suitable alternative to them.


回答1:


Once you query for your file by id you can use the function getWebContentLink() to get the download link of the file (eg. $file->getWebContentLink() ).




回答2:


I think you're placing too much emphasis on the word "method".

There is still a link to export a file, it's https://www.googleapis.com/drive/v3/files/fileIdxxxxx/export&mimeType=xxxxx/xxxxx. Make sure you URL encode the mime type.

Eg

https://www.googleapis.com/drive/v3/files/1fGBQ81haNU_nEiC5GITZD3bxT0ppL2LHg-C0ubD4Q_s/export?mimeType=text/csv&access_token=ya29.Gmo0BMvO-pVEPKsiD9j4D-NZVGE91MChRvwOcBSg3cTHt5uAClf-jFxcovQScbO2QQhwHS95eSGW1eQQcK5G1UQ6oI4BFEJJkntEBkgriZ14GbHuvpDL7LT2pKA--WiPuNoDDIuZMm5lWtlr

These links form part of the API, so the expectation is that you've written a client that sends authenticated requests, and deals with the response data. This explains why, if you simply paste the link into a browser without an access_token, it will fail. It also explains why the filename is export, ie. it isn't intended that your client would ever use a filename, but rather it should receive the data as a stream. This SO answer discusses the situation in more detail How to set name of file downloaded from browser?



来源:https://stackoverflow.com/questions/43553926/google-drive-api-v3-there-isnt-any-way-to-get-a-download-url-for-a-google-doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!