Thumbnails of shared Google docs give 404 - google-drive-api

When I request file info via the Google Drive API for a Google Docs file that can be viewed by anyoneWithLink I get a thumbnailLink that produces a 404 Error if you view it as an anonymous user. Is there a way to get a thumbnail for such shared files that can be viewed by anyone? For Drawings I found https://docs.google.com/drawings/d/FILE_ID/image?w=WIDTH, but I didn't find anything for the other file formats.

Please use the thumbnailLink provided on the file resource. https://developers.google.com/drive/v2/reference/files#resource

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

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.

Get 404 error when accessing a public file's meta data on Google Drive

I have a file which is shared public.
https://drive.google.com/file/d/1SuYrjwWJKlNmSEr6CpcOVFUT1MpGqLe3/view
But when I use Google API to access the file's meta data, the result is 404 not found.
https://www.googleapis.com/drive/v3/files/1SuYrjwWJKlNmSEr6CpcOVFUT1MpGqLe3
Can someone tell me the reason of this behavior?
If you want to access via Drive API a file that is not located on your Drive, you need to set
supportsAllDrives to true:
https://developers.google.com/drive/api/v3/reference/files/get?apix_params=%7B%22fileId%22%3A%221SuYrjwWJKlNmSEr6CpcOVFUT1MpGqLe3%22%2C%22supportsAllDrives%22%3Atrue%7D

Google Docs thumbnail link returns 404

I have questions regarding the behavior of the Google Drive v3 API files resource thumbnailLink property for Google Docs files. There are two different types of thumbnail links created depending if the file is a Google Doc or other type of file. When going to the thumbnailLink URL for a Google Doc I always get a 404 response, unless I open it on a Chrome browser where I am logged in with the same Google account.
My questions about the thumbnail link are:
What is the minimum OAuth scope required to access it?
What are the exact criteria required for me to have see the thumbnail image in a browser instead of getting a 404?
Is it possible to make the link to google doc thumbnails publicly accessible?
If not, is there some alternative way to build the thumbnail url?
I preferably want to avoid this answer as it will either expose the access key or add complication by setting up a proxy.
This is the format of the google doc thumbnail link: https://docs.google.com/feeds/vt?gd=true&id=1TjchRkZ0MxurnBkKNitDtoMQiyJUV6rv0z9HQ7kHEg0&v=3&s=AMedNnoAAAAAXaTCgqfi_sjlT4SAGAEd_ZABNBoSvMnM&sz=s220
Other types of documents that use the following type of thumbnail link are publicly available: https://lh3.googleusercontent.com/JkrFnbTt-H1rJb-kXhjRsQI3NiiVu7DYD_L7kBLPvBzu4-ieXNB6Jb-ukQpHwujlzybaeirEyV0=s220
I see this behavior in the Drive v3 "Try This API" screen with all scopes authorized. I go to the thumbnailLink immediately after getting the response so it is not expired.
Edit: The webViewLink works for me in the case of a google slide. So I am allowed to see the full contents publicly using that link but still get a 404 with the thumbnaiLink. Based on that inconsistency this seems like a bug, not a security or permissions issue.
This is not a bug. You are not supposed to be able to access the thumbnailLink if you don't have access to the file's content, as indicated here.
As a workaround, I would propose your app or whoever has access to the thumbnail links to upload the thumbnails to Drive, so that users can access these Drive links. Anyway, I don't know the details of your project so I'm not sure this would be a feasible or desired workaround.
The reason why you are getting a 404 is that the binary documents' thumbnails are publicly accessible. If you can get your hands to the ThumbnailLink field of Google File, you can download the thumbnail images using a simple WebClient without any authentication.
However, if you're trying to download a Google Document's thumbnail image using WebClient that will give you a 404 error, because Google expects that you download this ThumbnailLink URI using an authenticated connection.
I had faced a similar problem and was able to resolve it very easily. I use Google Drive API (version 3) for accessing the documents on Google Drive. The Google API has already authenticated my account which is how I am able to acquire the ThumbnailLink in the first place. But instead of using WebClient, use the DriveService's HTTPClient to download the thumbnail. The HTTPClient in DriveService creates an authenticated HTTP request allowing you to download thumbnails for Google Documents as well.
private async Task<bool> DownloadThumbnail(string thumbnailURL, string localFile)
{
try
{
var client = DriveService.HttpClient;
var response = await client.GetAsync(thumbnailURL);
using (var fs = new FileStream(localFile, FileMode.CreateNew))
await response.Content.CopyToAsync(fs);
return true;
}
catch (Exception ex)
{
// log error
return false;
}
}
Get thumbnail link with scopes drive and drive.readonly
It's private data Token or login required
https://drive.google.com/thumbnail?authuser=0&sz=w280&id=[fileid]
sz = size
w = width

When/why can file.downloadUrl be empty for a PDF file selected from Google Drive?

Using js file picker to select PDF files from Google Drive.
I'm encountering an instance when the downloadUrl is undefined.
I don't understand what can cause this.
I know downloadUrl can be empty for native Google formats, but these are PDF files.
I'd like to know what scenarios (except native Google formats) can cause an empty downloadUrl.
Thanks
You should use the Drive API to fetch the picked files' metadata in order to retrieve the downloadUrl. https://developers.google.com/drive/v2/reference/files/get
Native Google formats can be downloaded (converted and downloaded) via their exportLinks: https://developers.google.com/drive/v2/reference/files#exportLinks
The response I was receiving was 403.
And the scenario was user accessing "work" Drive (Google Drive for your domain)
Turns out Google Drive for your domain Administrators can restrict the permission to install third party apps.
https://support.google.com/a/answer/6105699?hl=en
User will still be able to allow access for the app and select files. Its just going to fail every time with a 403. Very confusing UX.