Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API get file Edit URL

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?

like image 353
corey Avatar asked Apr 11 '15 04:04

corey


Video Answer


2 Answers

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

like image 109
pinoyyid Avatar answered Oct 16 '22 20:10

pinoyyid


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();
  }
like image 35
Bik Avatar answered Oct 16 '22 20:10

Bik