0

I need to write a php script that will download all text files on a google drive folder to local folder. The code below is able to download all files on the gdrive but the content of the downloaded files is all wrong. The content seems to be an error log instead of actual content of file.

Sample content of file after download:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ]
  }
}

Got this code on the web:

<?php

require 'vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=D:\username\folder\credentials.json'); 

function downloadFilesFromFolder($folderId, $destinationPath, $service)
{
    // List all files in the folder
    $pageToken = null;
    do {
        $parameters = array(
            'q' => "'$folderId' in parents and mimeType='text/plain'",
            'fields' => 'files(id, name)',
            'pageToken' => $pageToken,
        );

        $results = $service->files->listFiles($parameters);

        if (count($results->getFiles()) == 0) {
            print "No text files found in the specified folder.\n";
        } else {
            foreach ($results->getFiles() as $file) {
                $fileId = $file->getId();
                $fileName = $file->getName();
                $fileDownloadUrl = "https://www.googleapis.com/drive/v3/files/$fileId/export?mimeType=text/plain";

                $response = $service->getClient()->getHttpClient()->request('GET', $fileDownloadUrl, ['sink' => "$destinationPath\\$fileName"]);

                if ($response->getStatusCode() === 200) {
                    echo "Downloaded: $fileName\n";
                } else {
                    echo "Failed to download: $fileName\n";
                }
            }
        }

        $pageToken = $results->getNextPageToken();
    } while ($pageToken);
}

$folderId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$destinationPath = 'D:\\username\\folder\\';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(Google_Service_Drive::DRIVE);

$service = new Google_Service_Drive($client);

downloadFilesFromFolder($folderId, $destinationPath, $service);

I need to download the files from gdrive with its actual content.

ebg
  • 1
  • 1
  • Which part of "missing a valid API key" was puzzling you? Presumably the credentials you supplied are not valid – ADyson Jul 19 '23 at 11:07
  • I'm new with google cloud stuff but the credentials would be coming from `credentials.json` right? It was generated and downloaded when the service account was created so I'm not sure how it could be wrong since I did not modify the file. – ebg Jul 19 '23 at 11:32
  • Ok. Next thing I suspect is that when you do `->getHttpClient()->request` rather than using one of the ready-made functions, it possibly does not pass any credentials automatically in the request. You likely have to set the relevant header yourself. I could be wrong but that's my guess. – ADyson Jul 19 '23 at 11:57
  • Check [this](https://stackoverflow.com/a/76397165/15211203) stackoverflow response. It's Python but it should give you an idea of what to do for PHP. Note the part of the response which talks about the requirement for using a service account to access files on Google drive – NoCommandLine Jul 19 '23 at 19:30

0 Answers0