I'm having issues trying to get the download link of a file using Google Drive API. It says the following:
thrown in ...\google-api\src\Http\REST.php on line 134
Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 403,
"message": "The user does not have sufficient permissions for this file.",
"errors": [
{
"message": "The user does not have sufficient permissions for this file.",
"domain": "global",
"reason": "insufficientFilePermissions"
}
]
}
}
Function:
/**
* Downloads a file using its URL
* @param string $fileId
* @return string link to download the file.
*/
public function downloadFileDriveServer(string $fileId) {
$this->setPermissionPublic($fileId);
/**
* @var \Google\Service\Drive\DriveFile $file
*/
$file = $this->service->files->get(
$fileId,
array('fields' => 'webContentLink')
);
return $file->getWebContentLink();
}
Inner function:
/**
* Tries to set the file permissions to public.
* @param string $fileId id of the file.
* @return void
*/
private function setPermissionPublic(string $fileId) {
$permission = new \Google\Service\Drive\Permission();
$permission->setRole('reader');
$permission->setType('anyone');
$this->service->permissions->create($fileId, $permission);
}
How it's beeing called:
echo $fileController->downloadFileDriveServer($fileId);
What I've tried: Change the local and the cloud scopes.
My local (in code) Scope: https://www.googleapis.com/auth/drive.
const SCOPE = \Google\Service\Drive::DRIVE;
Inside Google Cloud console, I have basically every scope of Google Drive like:
- ./auth/drive
- ./auth/drive.metadata
- ./auth/drive.metadata.readonly
- ./auth/drive.readonly
The project is internal use only and it's working for listing files, creating directories and deleting files.
Might be relevant: I'm not using frameworks and I've seen some questions right here and I don't understand yet where to use a:
GET https://www.googleapis.com/drive/v3/files/fileId?alt=media
I know what a GET is, but don't know where to use this "GET" line above.
Any support to enable my files to be downloaded would be appreciated. Thanks a lot.