Search code examples
phpdownloadgoogle-drive-api

Google Drive API: Why with a service account takes me to a Google drive sign in Window when downloading a file?


I have a script for uploading files and downloading them with a Google service account. This looked to be perfectly working, but now it's not when downloading. When clicking the link to download the file instead of begining the download a Google Drive Sign in page appears. To be honest Im not aware of making any changes in the PHP code of Google Cloud configuration. I've checked both and I don't see any change or something wrong. I must say Im not an expert.

Thanks in advance.

I've reviewed the code and Google configuration. I've expected to upload and download files from php code to Google Drive. The results is that upload files well but when download these files a Google Drive sign in window appears, instead of downloading the file.

require_once 'google-api-php-client/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=googlecredentials-path-file.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive.file']);
try {
   $service = new Google_Service_Drive($client);
  $google_file = new Google_Service_Drive_DriveFile();
  
  $file_path = 'buho.jpg';
    
  $google_file->setName($file_path);
  $google_file->setParents(['Google-Drive-folder-id']); 
  $google_file->setDescription('File uploaded from a PHP application');
  $google_file->setMimeType('image/jpeg');
  $result = $service->files->create(
    $google_file,
    array(
      'data' => file_get_contents($file_path),
      'mimeType' => 'image/jpeg',
      'uploadType' => 'media'
    )
  );
  echo '<a href="https://drive.google.com/open?id=' . $result->id . '" target="_blank">' . $result->name .'</a>';

Solution

  • you can generate a link, which when used, it will allow access without login. note: access to anyone with the link

    <?php
    
    require_once "google-api-php-client/vendor/autoload.php";
    putenv("GOOGLE_APPLICATION_CREDENTIALS=googlecredentials-path-file.json");
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->setScopes(["https://www.googleapis.com/auth/drive.file"]);
    try {
        $service = new Google_Service_Drive($client);
        $google_file = new Google_Service_Drive_DriveFile();
    
        $file_path = "buho.jpg";
    
        $google_file->setName($file_path);
        $google_file->setParents(["Google-Drive-folder-id"]);
        $google_file->setDescription("File uploaded from a PHP application");
        $google_file->setMimeType("image/jpeg");
        $result = $service->files->create($google_file, [
            "data" => file_get_contents($file_path),
            "mimeType" => "image/jpeg",
            "uploadType" => "media",
        ]);
        // Set permissions to create a shareable link
        $permission = new Google_Service_Drive_Permission([
            "type" => "anyone",
            "role" => "reader",
        ]);
        $service->permissions->create($result->id, $permission);
    
        // Get the shareable link
        $file = $service->files->get($result->id, ["fields" => "webViewLink"]);
        $shareableLink = $file->webViewLink;
    
        echo '<a href="' .
            $shareableLink .
            '" target="_blank">' .
            $result->name .
            "</a>";
    } catch (Exception $e) {
        // handle exception
    }
    

    Download file from url

    <?php
        // add your shareable link here as the parameter.
        $fileContent = file_get_contents($shareableLink);
        $localFilePath = "./save-to-file-here.jpeg"
        if ($fileContent !== false) {
            // Save the file locally
            $result = file_put_contents($localFilePath, $fileContent);
    
            if ($result !== false) {
                echo "File downloaded successfully and saved as $localFilePath";
            } else {
                echo "Error saving the file locally.";
            }
        } else {
          echo "Error downloading the file content.";
       }