stereolegged
stereolegged

Reputation: 83

How to create a shortcut using Google Drive API?

I've been reading the Google Drive API documentation but I don't understand how to create a shortcut to an external file. So far I've figured out how to log in and upload files by doing HTTP POSTs to www.googleapis.com using openssl s_client from the command line in Linux.

The section Creating Google Drive shortcuts to external files on page How Drive Apps Work describes shortcuts like this: "Shortcuts behave similarly to files. They can be opened and created, indexed in search, and shared with other users. Unlike regular files, shortcuts do not contain any content, and when synced to a desktop are opened as URLs in the user's browser. Synced shortcut files are assigned the .glink extension."

This part here says that the shortcuts are opened in the browser at least when synced to a desktop. (It remains unclear to me what happens if you use Google Drive via a browser and try to click on a shortcut.) This also seems to say that shortcuts get a file extension .glink, but what does the part about syncing here mean? If I post a HTTP request to www.googleapis.com to create a shortcut, do I need to specify .glink as the file extension?

The page I mentioned says to go to Create a shortcut to a file for instructions on how to create a shortcut. That page tells me what mimetype to use and says to check the documentation for files.insert. On that page I can't find any information on how to create shortcuts.

So how do you create a shortcut? Is there a parameter to files.insert where you specify a URL to redirect to user to?

Upvotes: 4

Views: 3274

Answers (1)

Alain
Alain

Reputation: 6034

Google Drive shortcuts are used to expose a file which contents is stored in a third-party web app in a user's Drive.

When a user clicks on a shortcut, the user gets redirected to the registered third party website with the Google Drive File ID in the ?state query parameter as described in Open Files. It is up to the third-party app to use the provided Google Drive File ID to match its internal ID and get the content.

Creating a shortcut is done the same way as inserting a new file's metadata with the only exception that the mimeType needs to be set to application/vnd.google-apps.shortcut:

POST https://www.googleapis.com/drive/v3/files?key=[YOUR_API_KEY]
Authorization: Bearer [YOUR_ACCESS_TOKEN]

{
    "name": "A file's Shortcut",
    "mimeType": "application/vnd.google-apps.shortcut",
    "parents": [
        // Optional, unless you want to put shortcut inside of a folder
    ],
    "shortcutDetails": {
        "targetId": targetId,
        "targetMimeType": targetMimeType,
    }
}

This POST request will create a shortcut to your application. The Drive API will know which application to create the shortcut for thanks to the authorization header containing this information.

Upvotes: 6

Related Questions