Unable to get file download link (Google Drive APi, PHP)

I have created a php page that contains a list of files stored in a folder and a submit button. After clicking the button, the file is uploaded to Google Drive. I want to do this after successfully uploading a file to Google Drive, a link appeared to download this file, but I cannot do it.

In the code section, I have tagged some code snippets where I tried to do this. On my first try, I got a white screen, but the file was downloaded. I got a bunch of errors on my second try.

<?php
        session_start();
        $url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        $url = $url_array[0];
        require_once 'google-api-php-client/src/Google_Client.php';
        require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
        $client = new Google_Client();
        $client->setClientId('CLIENT-ID');
        $client->setClientSecret('CLIENT-SECRET');
        $client->setRedirectUri($url);
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));
        $text = '';

        const DRIVE = "https://www.googleapis.com/auth/drive";

        if (isset($_GET['code'])) {
            $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
            header('location:'.$url);exit;
        } elseif (!isset($_SESSION['accessToken'])) {
            $client->authenticate();
        }
        $files = array();
        $dir = dir('files');
        while ($file = $dir->read()) {
            if ($file != '.' && $file != '..') {
                $files[] = $file;
            }
        }
        $dir->close();
        if (!empty($_POST)) {
            $client->setAccessToken($_SESSION['accessToken']);
            $service = new Google_DriveService($client);
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $file = new Google_DriveFile();
            foreach ($files as $file_name) {
                $file_path = 'files/'.$file_name;
                $mime_type = finfo_file($finfo, $file_path);
                $file->setTitle($file_name);
                $file->setDescription('This is a .'.$mime_type.' document');
                $file->setMimeType($mime_type);
                $fileid = $file->getId();

                $service->files->insert(
                    $file,
                    array(
                        'data' => file_get_contents($file_path),
                        'mimeType' => $mime_type
                    )
                );
            }
            finfo_close($finfo);
            //First try
            $downloadUrl = $file->getDownloadUrl();
            header('location: '.$downloadUrl);exit;

        }

        function get($fileId, $optParams = array()) {
            $params = array('fileId' => $fileId);
            $params = array_merge($params, $optParams);
            $data = $this->__call('get', array($params));
            if ($this->useObjects()) {
              return new Google_DriveFile($data);
            } else {
              return $data;
            }
        }


        //Second try
        function downloadFile($service, $file) {
              $this->public;
              $downloadUrl = $file->getDownloadUrl();
              if ($downloadUrl) {
                $request = new Google_Http_Request($downloadUrl, 'GET', null, null);
                $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
                if ($httpRequest->getResponseHttpCode() == 200) {
                  return $httpRequest->getResponseBody();
                } else {
                  // An error occurred.
                  return null;
                }
              } else {
                // The file doesn't have any content stored on Drive.
                return null;
              }
            }

        include 'index.phtml';
?>

      


Made changes to the code given in the following example: https://developers.google.com/drive/v3/reference/files/get#try-it , but the error appeared after clicking the Submit button - "Spoofed fatal error: Object the Google_DriveFile class cannot be converted to a string. " Updated:

<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('xxx');
$client->setClientSecret('xxx');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$text = '';
$fileId = '';

const DRIVE = "https://www.googleapis.com/auth/drive";

if (isset($_GET['code'])) {
    $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
    header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
    $client->authenticate();
}
$files = array();
$dir = dir('files');
while ($file = $dir->read()) {
    if ($file != '.' && $file != '..') {
        $files[] = $file;
    }
}
$dir->close();
if (!empty($_POST)) {
    $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();
    foreach ($files as $file_name) {
        $file_path = 'files/'.$file_name;
        $mime_type = finfo_file($finfo, $file_path);
        $file->setTitle($file_name);
        $file->setDescription('This is a .'.$mime_type.' document');
        $file->setMimeType($mime_type);
        $fileid = $file->getId();
        $response = $service->$file->get($fileId, array(
          'alt' => 'media' ));
        $content = $response->getBody()->getContents();
        $service->files->insert(
            $file,
            array(
                'data' => file_get_contents($file_path),
                'mimeType' => $mime_type
            )
        );
    }
    finfo_close($finfo);
    header('location: '.$content);exit;

}

function get($fileId, $optParams = array()) {
    $params = array('fileId' => $fileId);
    $params = array_merge($params, $optParams);
    $data = $this->__call('get', array($params));
    if ($this->useObjects()) {
      return new Google_DriveFile($data);
    } else {
      return $data;
    }
}

include 'index.phtml';

      

+3


source to share





All Articles