lundi 27 juin 2016

Google Drive API get edit url (aka alternateLink) for file with PHP SDK

  • I've setup my project to use the PHP SDK for the Google Drive API
  • I'm using a refresh token to authenticate a user via OAuth2 (not using a service account, if it matters)
  • I am getting a list of files from a specific folder with this
    function:

.

function getDriveFilesForFolder($app){
    // Get the API client and construct the service object.
    $client = getClient($app); // function that gets an authorized client instance
    $service = new Google_Service_Drive($client);
    // we only want files from a specific folder
    $q="'".$app->constants->get('GOOGLE_DOCS_MAIN_FOLDER_ID')."' in parents";
    $optParams = array(
         'q' => $q
    );
    $results = $service->files->listFiles($optParams);
    if (count($results->getFiles()) == 0) {
        echo "No files found.<br>";
    } else {
        echo "Files:<br>";
        foreach ($results->getFiles() as $file) {
            //var_dump($file);
            echo '<br><br>';
            echo $file->getName()." (".$file->getId().") ";
        }
    }
}

This works and prints out the list of file names and their ids.


  • Now I need to get an editable link for each file and have that print out as well.
  • From my searching, what I want is the alternateLink which is documented here for the Rest endpoint
  • I see no function in Google_Service_Drive_DriveFile or Google_Collection which it extends that will return the alternateLink.

How do I get the alternateLink value from the $file object in my code using the PHP SDK?


Worth mentioning

  • These files are pre-existing and not created with the PHP SDK

Aucun commentaire:

Enregistrer un commentaire