Progger
Progger

Reputation: 61

Not receiving "webViewLink" in response?

After turning on Google Drive API access from the management console and getting my Client ID keys, I followed the sample code (using Python 2.7) and I am able to insert a folder, set the appropriate permissions (type=anyone,role=reader), and insert a text/html type file into the new folder.

However the JSON file resource objects I receive from executing insert on the drive service have no 'webViewLink' field! There are 'webContentLink' and 'selfLink' fields but 'webViewLink', which is necessary for static HTML publishing, seems to be missing.

Most perplexing. If this feature hasn't been turned on yet or if I need to configure my account settings to allow HTML publishing please let me know. Any other help would be most appreciated ;)

Upvotes: 2

Views: 2989

Answers (2)

Heitor Althmann
Heitor Althmann

Reputation: 367

The WebViewLink file property can be retrieved by doing something like this:

$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.

If you also need to configure file permissions, you can do something like:

$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

Upvotes: 1

Claudio Cherubino
Claudio Cherubino

Reputation: 15014

The webViewLink property is only returned for public folders, and not the single files inside such folders. You can use that as the base url to construct links to your files.

Upvotes: 1

Related Questions