0

I am trying to retrieve an image from a Google Drive that is part of our G Suite account.

I have created a service account and granted it domain-wide authority and I am passing the json file to the client that I create. I can successfully get the file metadata however when I try to download the actual file using the sample code, the call returns nothing:

$client = new Google_Client();
$client->setAuthConfig(getcwd() . '\credentials\API-Project-b1efa769fa8c.json');
$client->setApplicationName('API Project');
$client->setSubject('tdellett@georgesmusic.com');
$client->setScopes(SCOPES);

$fileURL = 'https://drive.google.com/file/d/1OwBr3dHakMYviIO9uCI-lkFkygU3pMB8/view?usp=sharing';
$URLParts = explode('/', $fileURL);
$find = array_search('d', $URLParts);
if ($find !== false) {
    $fileId = $URLParts[$find + 1];

    $service = new Google_Service_Drive($client);
    $response = $service->files->get($fileId, array('supportsTeamDrives' => 'true'));
    $fileName = $response->getName();
    $mimeType = $response->getMimeType();
    $contentLink = $response->getWebContentLink();

    $response = $service->files->get($fileId, array('alt'=>'media','supportsTeamDrives' => 'true'));
    $content = $response->getBody()->getContents();
    file_put_contents($fileName, $content);
}

Am I doing something completely wrong here?

TIA

  • How about trying the suggestions in this [SO post](https://stackoverflow.com/questions/40028169/google-drive-api-v3-downloading-files-in-php?rq=1)? Could it solve your problem? Also, have you tried the [example in the documentation](https://developers.google.com/drive/v3/web/manage-downloads#examples)? – MαπμQμαπkγVπ.0 Mar 07 '18 at 10:21
  • That's brilliant, thanks! That article didn't come up in my search. Works a peach. – Tony Dellett Mar 07 '18 at 14:24
  • You think this answer's your question? I will post it as answer. – MαπμQμαπkγVπ.0 Mar 08 '18 at 03:10

0 Answers0