How to view Google Drive file in browser - google-drive-api

There is file stored in Google Drive. It is needed to open this file in browser. Google API JS library has method gapi.client.drive.files.get. It allows to get file resource which contains downloadUrl. This url allows to download file and save it on disk. Query string of url contains parameter e=download. If value of this parameter is changed to 'view' (e=view) then this url can be used in order to open gif file in browser. Nevertheles if the same is made with url to txt file then file is downloaded anyway.
Is there Goggle url which can be used to open txt file in browser? How to get such url?

Use the webContentLink:
"A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials."

Related

Why does the webContentLink access I get through the http interface of the Google cloud disk list report 404

I obtained the file download url through the following interface, and the corresponding field is webContentLink, but why did I return a 404 status code when I accessed this download url. I have tested this problem only with some of these accounts. There is no idea to solve this problem now.
interface address:https://developers.google.com/drive/api/v3/reference/files/list?hl=en&authuser=1
According to their docs, WebContentLink is only available for files with binary content in Google Drive.
If you want to download all kind of file then use the files.get method with the ID of the file to download and set the alt=media URL parameter
Reference: https://developers.google.com/drive/api/guides/manage-downloads#download_a_file_stored_on_google_drive

Downloading files from Drive using alt=media with correct filename

I'm having trouble allowing files (non-shared) to be downloaded from a Google Drive account. I've created a listing using the php drive sdk and would like to provide authorised links to download the files using a generated access token. I've got downloads working with links like this:
https://www.googleapis.com/drive/v3/files/[fileid]?alt=media&access_token=[access_token]
The problem is that whenever a file is downloaded, it is named [fileid].[extension], rather than the real file name that appears in Drive.
I've tried adding the download="[real filename]" into the a link to suggest the correct filename, but it's being ignored in all the browsers I've tried.
I've got an alternative working that gets the file piece by piece server side and echoes it out as a file via php, but I'd prefer for downloads to be straight from Drive to the user.

Oauth2 Google Drive offline access not working for non-google app files?

Having a very frustrating issue with Google Drive API.
This is a Google App Engine Java environment.
I need to download a file based on an offline permission - the file is shared ("Can Edit") with the user who gives the offline permission. I am requesting full https://www.googleapis.com/auth/drive scope. I store off the refresh token this generates.
The app then uses this refresh token to get an access token, which it uses to try and download a file. This works file if the file is a Google Apps file (a Google document, spreadsheet etc.) but gives a 401 if the file is an uploaded file with content (e.g. a PDF file, Word file etc.). I can confirm this at a simple level by appending the share urls with ?access_token=xxxx - this works for the Google Apps file but not for an uploaded normal file, which 401's on the webcontentlink url. Note that the https://www.googleapis.com/drive/v2/files/ endpoint responds correctly with the metadata for the uploaded file using the access token that subsequently fails on the download call.
The full html response from either a direct url call (with access_token=) or a servlet call is
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Is there a known issue with offline Google auth on non native files in Drive?
Based on the descriptions of file attributes, you should probably be using the downloadUrl attribute rather than webContentLink.
downloadUrl - Short lived download URL for the file. This is only populated for files with content stored in Drive.
webContentLink - A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.

Copy a file from Google Drive to my own server

If you remember, I'm trying to integrate Google Drive within our website, which is built on Elgg. Elgg already has its native file management system.
What we would like to do is to copy a file from Drive to our server, you know, kind of : Send to My Files. The problem is that I don't see any URL in the file metadata indicating where the file is physically stored.
I can see the copy function in Google Drive SDK but I don't think it allows to copy the file on our own server. Unless I've read it wrong.
Can you help me?
Thanks you.
If you are trying to save a file that has content stored in drive (e.g an image, pdf, etc.), the file's metadata should contain a downloadUrl that can be used to retrieve the file's content through an authorized GET request.
For Google Documents (Google Docs, Google Spreadsheets, etc.), the data is stored in a private format that cannot be understand by third party applications. In this case, your app will have to use one of the exposed exportLinks to export the document into a format understood by your application.

File Download using HTML 5 , Javascript and File API

Can we integrate the FileAccess API of HTML with normal download of Files using javascript.
We typically download a file by the below way:
var fileUrl='../File.doc';
window.open(fileUrl,'Downloading');
Now I believe that the file would be downloaded to tempFolder and the folder designated by us for download.
But is it possible for us to download the file to a sandboxed location as mentioned in WWC draft on HTML 5 File API.
If this is possible, I believe "need for a way to delete downloaded files" of my previous question would be solved.
There's a new download attribute you can add to links that tells the browser to download the resource rather than navigating to it. That downloads the file to whatever "Downloads" folder the browser uses by default. You cannot programmatically delete files that are downloaded. That would be a security risk.