google-api-php-client: Downloading a document using export link is not working

When trying to download a file without a download link using it’s export link new library give authentication error and says that the file does not exist. File is owned by the user which uses the API. In old version 0.6.* this problem did not occure.

Here’s a piece of code:

try {
            /* @var Google_Service_Drive_DriveFile $file */
            $file = $service->files->get($id);
        } catch (Google_Exception $e) {
            $this->_setError(sprintf('Error retrieving file data by id "%s". Error thrown: %s', $id, $e->getMessage()));
            return false;
        }

        $downloadUrl = $file->getDownloadUrl();

        //Native Google Drive files do not have a download link, use (standard) export link.
        if (!$downloadUrl) {
            $exportLinks = $file->getExportLinks();

                switch ($file->getMimeType()) {
                    case self::FILE_TYPE_SPREADSHEET:
                    case self::FILE_TYPE_FORM:
                        $exportFormat = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                        break;

                    case self::FILE_TYPE_DOCUMENT:
                        $exportFormat = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                        break;

                    case self::FILE_TYPE_PRESENTATION:
                        $exportFormat = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                        break;

                    default:
                        $keys = array_keys($exportLinks);
                        $exportFormat = $keys[0];
                        break;
                }

            $downloadUrl = $exportLinks[$exportFormat];
        }

        if ($downloadUrl) {
            $request = new Google_Http_Request($downloadUrl);
            $clientAuth = $this->_getClient()->getAuth();
            $clientAuth->sign($request);
            $httpRequest = $clientAuth->authenticatedRequest($request);

                        /* if the file is not created via API responce code is always 404 */
            if ($httpRequest->getResponseHttpCode() == 200) {}
               }
}

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

I cannot reproduce with the dev version of the client. I created a Spreadsheet from the Google Drive web interface, noted down the id of the sheet, executed the following code:

$id = '<id_of_the_spreadsheet>';
$file = $service->files->get($id);
$exportLinks = $file->getExportLinks();
$downloadUrl = $exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
$request = new Google_Http_Request($downloadUrl);
$httpRequest = $client->getAuth()->authenticatedRequest($request);

And I get a 200 and the content of the file in the ResponseBody. I tried using both Google_IO_Curl and Google_IO_Stream. Othen config params that might be relevant:

const GZIP_DISABLED = true;
const GZIP_ENABLED = false;
const GZIP_UPLOADS_ENABLED = true;
const GZIP_UPLOADS_DISABLED = false;