how to use PickerBuilder.setRelayUrl(string) in google picker API? - google-drive-api

I need to get a shareable link to google drive photos and files. Do I need to know this method can be used for that or what can be used for that?

You can see the defined use for setRelay in Picker Class Reference:
PickerBuilder.setRelayUrl(string) - Set the relay URL, used for gadgets.rpc.
Picker.setRelayUrl(string) Specify a relay URL to circumvent cross-domain issues.
However to answer your question, you might be looking for webViewLink:
A link only available on public folders for viewing their static web
assets (HTML, CSS, JS, etc) via Google Drive's Website Hosting.
This property is returned from a successful API call like files.get or files.list.

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.

Google Docs viewerng api reference

I am trying to show a file hosted in a public site inline on my web page using google docs by providing the following URL to an iframe.
https://drive.google.com/viewerng/viewer?url='+link+'&embedded=true
where link is the path to the file I want to show.
I am facing some issues with showing files which require user authentication.
Hence would like to see reference documentation for the API. I searched for it online was unable to find any reference documentation for the following service end point.
"https://drive.google.com/viewerng/viewer"
Let requests originating from this Google service pass your authentication
Include a secret parameter in the link to confirm that an authenticated user (of your site) is requesting the document (via viewerng)

Unable to serve download links in google apps script

UPDATE: I have found a solution. This doesn't necessarily address every case, so I will leave the question open for a short time in case someone can enlighten me more. I solved it by changing the format of the url: Google Drive allows this format for downloading files:
https://docs.google.com/uc?export=download&id=FILE_ID
So I don't know if this is a problem for other URL's; nor actually exactly why the .getDownloadUrl() doesn't work ... maybe someone can explain. But for now this seems to work in the browsers that I can test ...
I have a simple WebApp script which I run on a Google Site by adding the Apps Script gadget. The gadget runs exactly as the Forms example on:
https://developers.google.com/apps-script/guides/html/communication#forms
The gadget is designed to do the following: when the page is loaded, a form is returned, and the user must enter a license key to get a link to download a product. My code serves the form OK, and gets the form submit OK; and it then validates the key, and if valid, sends back a link to download. All that works fine; and the problem is that no matter what I try to return for the download link, the caja iframe wrapper is preventing the click on the link from actually downloading the file.
My preferred URL to return is in fact via the Drive API: the download file is on the Google Drive, and I get the download link like so:
DriveApp.getFileById(downloadFileId).getDownloadUrl()
But when the returned link is clicked inside that caja iframe generated for the WebApp gadget, nothing happens. I have tried a few other URL formats pointing to that file on the Drive, but nothing is working for a download.
Is this possible?
.getDownloadUrl() method returns a temporary URL that can be used to download the file. This URL is valid only for a short period of time, after which it expires and does not return the file any more - that is probably why the links in your web app do not work. Can't remember exactly how long the URL is valid for, but I think it could be as short as 5 minutes.
Permanent download URL is stored in another file property: webContentLink. However, this property is not (yet) available through Google Apps Drive Service - you must use Advanced Drive Service to access it. You can enable Drive API under Advanced Google services in your script. After it is enabled, you can use it like so:
var file = Drive.Files.get(FILE_ID_HERE);
var dlUrl = file.webContentLink;
This will return the link just like the one you found and posted in your update. An advantage of using the Drive API to get the link, instead of hard-coding it, is that if Google ever changes the format of that URL, your code using Drive API to get the link will continue to work, while hard-coded links will not.
Full Drive Web API reference (what Advanced Drive Service uses) is at https://developers.google.com/drive/v2/reference/.

How do I use exportlinks from javascript to download a Gdoc using Drive SDK?

I'm trying to download content from a Google docs file using Javascript using the Drive SDK.
I retrieve the metadata, and parse the exportLinks to get a URL
https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=html
My code is similar to Google Drive using JavaScript: Handling file content except I'm using exportLinks:text/html instead of downloadUrl.
If I try to retrieve that URL, I get "405 OPTIONS method not allowed".
(as an aside, if I download a non-docs file using the downloadUrl, all is OK).
So I'm guessing that either:-
a) I shouldn't be using Drive to retrieve Docs files
b) I should be using the exportLinks is some other fashion, or
c) I need to implement the same iframe workaround that is used for upload here Does https://www.googleapis.com/upload/drive/v2/files really support CORS?
Export links do not currently support CORS. It is something we are working on, so sorry about that.
If you can use the iframe hack for now, until we get it working, then please do.

Is is possible to get a permanent URL to a file uploaded to Google drive?

I can't find the answer to this in the API docs or elsewhere. I see in the docs says you can get a downloadURL of a file, but it refers to it as a 'short lived URL'. What does that mean?
I need to upload images and get a permanent URL of that image that is the direct URL than can be embedded into emails or web docs etc. Is that possible?
Thanks
Yes, you can get a permalink to any file hosted on Google Drive in a public folder. Just note the folderID:
and paste it to the following URL:
http://googledrive.com/host/<folderID>/<filename>
or you can create a short custom alias using G Drives: http://gdriv.es/<alias>/<filename>
https://developers.google.com/drive/v2/reference/files#webContentLink
downloadUrl is short lived, but webContentLink is a permanent link. It's odd that one is a URL and the other is a "link", but I guess it probably has to do with the additional query params in the strong. Maybe that makes it not just a URL? :)
That property is only available for files that are publically readable, so you may have to use the SDK/API to set the permissions first.
In case someone encounter the same problem and don't want to do with deprecated Google Drive solution above and requires frequently changed files with exact same url, you can use Dropbox. Steps:
Download the program and share a folder (tutorial)
Copy a file into that folder; wait for sync with Dropbox
Go to your dropbox.com account
Share your file by button, get something like:
https://www.dropbox.com/s/d28d8scvr3rfy48/foo.exe?dl=0
Rename it to:
https://dl.dropboxusercontent.com/s/d28d8scvr3rfy48/foo.exe?dl=0
Now you have a public, unchanged url for all your foo.exe changes.
Enjoy, don't waste time with sites offering you file hosting and they change the URL for same files.
If you share a file publicly in GDrive, you can use the following link to make the file download directly:
https://drive.google.com/uc?export=download&id=FILEID
The problem happens when you want to change the file (for version controlled download, in instance) and keep the same downloading link.
You can use a URL Shortener like bitly.com and use a Branded Bitlink to change the destination URL keeping the input URL.
I hope it works, good luck!
It seems if you have file ID you can access it with such url:
https://drive.google.com/open?id=file-id-here
If file is public, you'll see it immediately but if not, you're be asked to login
I don't know how often this changes but I was looking for a dynamic solution where I wouldn't need to manually upload to another site and the provided solutions weren't working.
Here's the url that worked for me:
https://lh3.google.com/u/0/d/<file_id>
Where file_id is the getId() using the google apps script drive API. Secondly the file must be shared publicly like posted previously
Secondly I imagine the lh3 are different cdn's though I cannot confirm since lh1/2 don't work and everything higher than 3 redirects to lh3
I don't know if such API for google drive exists, but I think you should also have a look at dropbox,
which will meet all the demands that you mention in your question.
Also there are some very cool applications developed just for dropbox, like pancake.io which lets you share links of text, html files, in an html page format.
that + permanent link is always there.
You can use gdriveurl.com, it got updated and now allows users to log in with their google drive account and share the list of files instantly, getting for each file "View" & "Download" short permalink.
There's also the old solution (login to Google Drive, set everything to "Public on web", copy share link, convert it into the gdriveurl.com textarea in the homepage), but it's just a waste of time, you should try "MyDrive" api.
Not sure if the link generated here is guaranteed to be permanent, but Google Drive Direct Link Generator does the job of generating a direct download link.
Just make sure your file's sharing setting is set to Anyone with the link