26

I am working on an app using Google Drive. I want the user to be able to share files by link, setting the permissions to anyone and withLink as described in the Google Developers documentation.

However, I cannot figure out what link to share. When I share a file in the Google Drive browser interface, I see the Link to share in the format:

https://docs.google.com/presentation/d/[...]/edit?usp=sharing

A link in this format is not part of the file resource object, nor it is returned from the http call setting the permissions. I hope someone can explain to me how to get this link through the REST api?

Jawa
  • 2,336
  • 6
  • 34
  • 39
Jon Saalbach
  • 365
  • 1
  • 4
  • 7

5 Answers5

26

You can use the alternateLink property in the File resource to get a link that can be shared for opening the file in the relevant Google editor or viewer:

https://developers.google.com/drive/v2/reference/files

Update

[With API V3](https://developers.google.com/drive/api/v3/reference/files it is suggested to use the webViewLink property.

wpp
  • 7,093
  • 4
  • 33
  • 65
Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
16

To actually enable link sharing using Google Drive API:

drive.permissions.create({
  fileId: '......',
  requestBody: {
    role: 'reader',
    type: 'anyone',
  }
});

Get the webLinkView value using:

const webViewLink = await drive.files.get({
    fileId: file.id,
    fields: 'webViewLink'
}).then(response => 
    response.data.webViewLink
);

rzymek
  • 9,064
  • 2
  • 45
  • 59
  • 1
    This helped me a lot. Bear in mind that you could apply that permission to the whole folder so every single file will be shareable by the webViewLink – Gonzo345 Apr 09 '21 at 11:28
4

In my case using the PHP Api v3, for the link to be non-empty you must define that you request this field... and if you have the right permissions:

so something like this:

$file =self::$service->files->get("1ogXyGxcJdMXt7nJddTpVqwd6_G8Hd5sUfq4b4cxvotest",array("fields"=>"webViewLink"));
Miguel
  • 3,349
  • 2
  • 32
  • 28
  • I'm also on api v3 (php). I do get `webViewLink` in the API's response. But opening that link in incognito takes me to a login screen. Is there a way in the request of the file to say in effect "give me a link that is public to the world, good for 1 access only"? (I'm storing images in an account that is not the web user's.) – jbobbins Dec 13 '17 at 18:43
  • 1
    yes, you should change the file permission after getting the file ID. – Miguel Dec 14 '17 at 13:41
3

Here's a practical example on how to get the WebViewLink file property (A.K.A. File edit link):

$file = $service->files->get($file_id, array('fields' => 'webViewLink'));
$web_link_view = $file->getWebViewLink();

OR

$sheetsList = $drive_service->files->listFiles([
  'fields' => 'files(id, name, webViewLink, webContentLink)',
]);

$web_link_view = $sheetsList->current()->getWebViewLink();

Pay attention that you should load the file specifying which fields you wanna bring with it (In this case, webViewLink). If you don't do that, only id and name will be available.

In case you need to adjust the file sharing settings, this is how you do it:

$permissions = new \Google_Service_Drive_Permission();
$permissions->setRole('writer');
$permissions->setType('anyone');

$drive_service->permissions->create($file_id, $permissions);

Possible values for setRole() and setType() can be found here: https://developers.google.com/drive/api/v3/reference/permissions/create

Heitor Althmann
  • 307
  • 2
  • 5
1

For python, I only needed to get the file "id". Then "created" the link like this:

def create_folder(folder_name, folder_id):
    """Create a folder and prints the folder ID and Folder link
    Returns : Folder Id

    """

    try:
        # create drive api client
        service = build("drive", "v3", credentials=creds)
        file_metadata = {
            "name": folder_name,
            "mimeType": "application/vnd.google-apps.folder",
            "parents": [folder_id],
        }

        file = (
            service.files().create(body=file_metadata, fields="id").execute()
        )
        id = file.get("id")
        print(
            f'Folder ID: "{id}".',
            f'https://drive.google.com/drive/u/0/folders/{id}',
        )
        return id

    except HttpError as error:
        print(f"An error occurred: {error}")
        return None
rubbles
  • 11
  • 3
  • Thanks for the quick fix. It's important to note that this URL could change at any time, so if it is hardcoded, the change would be a breakage, instead. So collecting the value programmatically would be the best bet long-term. – ingyhere Dec 12 '22 at 18:08