Search code examples
phpgoogle-drive-apigoogle-client

google drive get shareable link after setting permission


I want to generate a shareable link for a zip file on google drive via the php client provided by google.

Using Google Drive's php client I am able to set a permission (confirmed by viewing the file permissions in drive gui):

    $config = [
        'type' => 'anyone',
        'role' => 'reader',
        'withLink' => true,
            //'allowFileDiscovery' => false
    ];
    $tempPermission = new Google_Service_Drive_Permission($config);
    $ret = $this->service->permissions->create($file, $tempPermission);
    echo '<pre>';
    print_r($ret);
    $files = $this->service->files->get($file);
    print_R($files);

However I am not sure where to go from here to get the physical url.

This is the output of the file object after the perm is set:

Google_Service_Drive_DriveFile Object
(
    [collection_key:protected] => spaces
    [appProperties] => 
    [capabilitiesType:protected] => Google_Service_Drive_DriveFileCapabilities
    [capabilitiesDataType:protected] => 
    [contentHintsType:protected] => Google_Service_Drive_DriveFileContentHints
    [contentHintsDataType:protected] => 
    [copyRequiresWriterPermission] => 
    [createdTime] => 
    [description] => 
    [driveId] => 
    [explicitlyTrashed] => 
    [exportLinks] => 
    [fileExtension] => 
    [folderColorRgb] => 
    [fullFileExtension] => 
    [hasAugmentedPermissions] => 
    [hasThumbnail] => 
    [headRevisionId] => 
    [iconLink] => 
    [id] => 1CU_nTm06ATlf2s54890oY62NyEzCGgyf
    [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
    [imageMediaMetadataDataType:protected] => 
    [isAppAuthorized] => 
    [kind] => drive#file
    [lastModifyingUserType:protected] => Google_Service_Drive_User
    [lastModifyingUserDataType:protected] => 
    [md5Checksum] => 
    [mimeType] => application/zip
    [modifiedByMe] => 
    [modifiedByMeTime] => 
    [modifiedTime] => 
    [name] => restorepoint_2019_08_02_07_32_57.zip
    [originalFilename] => 
    [ownedByMe] => 
    [ownersType:protected] => Google_Service_Drive_User
    [ownersDataType:protected] => array
    [parents] => 
    [permissionIds] => 
    [permissionsType:protected] => Google_Service_Drive_Permission
    [permissionsDataType:protected] => array
    [properties] => 
    [quotaBytesUsed] => 
    [shared] => 
    [sharedWithMeTime] => 
    [sharingUserType:protected] => Google_Service_Drive_User
    [sharingUserDataType:protected] => 
    [size] => 
    [spaces] => 
    [starred] => 
    [teamDriveId] => 
    [thumbnailLink] => 
    [thumbnailVersion] => 
    [trashed] => 
    [trashedTime] => 
    [trashingUserType:protected] => Google_Service_Drive_User
    [trashingUserDataType:protected] => 
    [version] => 
    [videoMediaMetadataType:protected] => Google_Service_Drive_DriveFileVideoMediaMetadata
    [videoMediaMetadataDataType:protected] => 
    [viewedByMe] => 
    [viewedByMeTime] => 
    [viewersCanCopyContent] => 
    [webContentLink] => 
    [webViewLink] => 
    [writersCanShare] => 
    [internal_gapi_mappings:protected] => Array
        (
        )

    [modelData:protected] => Array
        (
        )

    [processed:protected] => Array
        (
        )

)

Bonus points if anyone can figure out how to set the permission with an expiration from one hour from the time of generation (couldn't find any info on this in the docs regarding the php client).


Solution

  • I found a solution. Needed to do this:

        $files = $this->service->files->get($file,array("fields"=>"webViewLink"));
        print_R($files);
    

    Which populates: webViewLink