Downloading files from Drive using alt=media with correct filename - google-drive-api

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.

Related

How to get a direct link from Google Drive?

I know it is possible to get a link that will initiate a download of a file from Google Drive. But this is not exactly what I am looking for. I want to be able to have the link of a file with its extension.
For example, it is possible to do this with Dropbox. I am able to get a direct link if I change "www.dropbox.com" to "dl.dropboxusercontent.com". So if I have a video file, it will play on the browser's player instead of opening the page to download it.
With Google Drive I don't know how to do this. If I generate a direct link, it will then create a link that automatically starts a download. A direct link for a .txt file will not be rendered on the browser. It will be downloaded instead.
So, it is possible to have a direct link to a file in Google Drive that is not the direct link that starts downloading automatically, but instead with the directory/file.ext?
There are several things you need to understand about how the Google drive api works.
When you do a file.get with the Google Drive api it returns a file resource this is the response for the file itself. The information about it that google is willing to share with us. There are two fields here you may find interesting
The first thing you should know is that a file has one or the other of these links not both. If the file is a binary type for example an image you will be given a webContentLink which can be used to download the file, If its say a google sheet then you will be given a webViewLink which can be used to view the file in Google Drive web application.
Which link you get depends entirely upon the type of file it is. No matter which link you get you will still need to have permission to access this file. So whoever clicks the link must have at the very least read permission to the file.
On the Google drive web application we can create links which can be shared with anyone and allow anyone to "access" there is no way to create these links VIA the api.
What you wish to do is out of scope for the Google drive api, probably due to security reasons. Also the simple fact that google drive api is not a file service api it a file storage system. THere is a difference.

Can download files (without file name or extension), but unable to see them?

I'm using the Google Drive API to generate a downloadable link for a file that looks like this:
https://www.googleapis.com/drive/v3/files/{file_id}?access_token={access_token}&alt=media
The problem is files are being downloaded without name and without extension.
If I add the extension manually after the file downloads it still works, but it's bad to tell my users they have to do that every time, and the user would have to know which extension the file actually has.
If I call https://www.googleapis.com/drive/v3/files/?access_token={access_token}
I can't see the files I've uploaded... I think it could be a problem related to having access to the files' metadata, but I did authorize the auth.files.metadata scope.
I generate the access token using the refresh token and obtained the refresh token from the Oauth Playground... I'm confident I used the proper credentials for my application, but I could triple-check if someone suggests this could be the problem.
What can I do to debug this?

I am trying to build a website that has a file manager on it. Is there any way to pull file paths and files from Google Drive?

I would also like to pull from Dropbox, Box, and iCloud Drive. If there is no way, I will try to work around it but I was just wondering. I would like to be able to see the whole drive (all files and folders accessible). Thanks.
Depending on what you exactly are looking to do, you can use Google Drive's API to create/open files on your Drive for a serious project. If you are just experimenting with an idea, you can host your site directly from Google Drive.
To display all the files including folders, use Files.list. If you want to specify which type of file you want to show, check Search Files.
To know more about working with folders and filepaths check Work with Folders

Uploaded file sometimes doesn't appear on the Google Drive web page

I've observed a strange behavior of Google Drive when I uploaded several files in a row to Google Drive by calling Google Drive APIs.
Everything seems working properly, I can get correct response back from API, such as file ID and URL. But when I looked into the folder where I uploaded files by web browser, files are sometimes missing.
This issue can be resolved by accessing the following URL which should not be necessary.
https://docs.google.com/file/d/<file ID>/edit
Could it be a Google Drive bug?
I've had the same problem. Usually the file would appear after some time. But sometimes not at all!
I've also noticed discrepancies between files shown in the web UI and files shown in the Google Drive directory on my PC. I would chalk this up to Drive's bugs.
Google drive still does this.
Files can take a long time to appear on the web gui. Similarly deleted files can remain on the web gui for a long time (and you get an error if you click on them).
I would really love a way to make drive more responsive.
İf you are uploading your files inside a folder then this might cause a problem. Make sure that parent folder exists for the file that you upload.

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.