Google Drive API get file Edit URL
Asked Answered
C

4

6

As of 04/20/2015 the Google Documents API v3.0 is deprecated, and will no longer function function on and after this date. So anyone using this API must switch to use the Google Drive API.

I have integrated the Google Drive API into my PHP application, but I can not find how to get a EDIT url for a file I have created or uploaded. Previously in the Google Documents API after uploading a file, the response would return a edit url that would be a direct url to edit the file.

I am using a service account that uses a generated key by my google developers account at https://console.developers.google.com. This means my application is making calls on behalf of my service account the developer account has created for me. A google drive service account CAN NOT be accesses by the Drive UI, because as a user you can not login to the account as you would a personal google account.

What I have done is shared my documents I have uploaded or created with my personal account, and the url google returns in the call is named "alternateLink" and it is formatted as such: https://docs.google.com/file/d/0B0nkjw07ekP9LUpuZG4tOHcxdkE/edit?usp=drivesdk

However when logged into the account I shared the above file with, it just goes to a viewer and not the "Google Docs Editor"

How can I get a file's edit link with Google Drive API?

Choriamb answered 11/4, 2015 at 4:1 Comment(0)
S
1

The link you are using is correct, so that's not the issue.

Syne answered 11/4, 2015 at 4:44 Comment(1)
Thank you for your response. You are correct, the "AlternativeLink" is the edit link. The issue was the uploadtype value, and mime-type when uploading the document file.Choriamb
C
1

The AlternateLink is the edit url, and my issue was the uploadType value and the mime-type for the document type when attempting to upload or create the document with the google drive api.

Choriamb answered 11/4, 2015 at 4:50 Comment(0)
M
1

The main problem was you have to set convert true in the time of upload. Without converting the file google will give you the link to view not to edit.

Here you will get file upload detials. Please check the below code i have only added the convert field:-

$file = new Google_Service_Drive_DriveFile();
  $file->setTitle($title);
  $file->setDescription($description);
  $file->setMimeType($mimeType);

  // Set the parent folder.
  if ($parentId != null) {
    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
  }

  try {
    $data = file_get_contents($filename);

    $createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => $mimeType,
      'convert' => true // To convert you file
    ));
    return $createdFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
Merrythought answered 24/7, 2015 at 10:30 Comment(1)
Yes Bik, you do need the convert set to true, and you need to set the correct file "uploadType" type. convert = true; uploadType = "media";Choriamb
M
0

The file edit URL can be found under the WebViewLink file property. You can retrieve it by doing something like:

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

// You could be using $seetList->getFiles() to loop through the files.
// Here we're just picking the current one (any file) to get the WebViewLink for.
print $sheetsList->current()->getWebViewLink();

In order to adjust file permissions you can use:

$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

Manvil answered 7/8, 2020 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.