title: Can't get the download link of file (Google Drive APi, PHP)
body: <p>I have created a page on php language that contains list of files, stored in a folder and button "Submit". After clicking on the button, the file is upload in Google Drive. I want to make that after a successful download of the file in Google Drive, a link appeared for downloading this file, but I can not do it.</p>
  
  <p>In the code section, I marked some code snippets where I tried to do this. In the first attempt, I got white screen, but file was uploaded. In the second attempt I got bunch of errors.</p>
  
  <pre><code>&lt;?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-&gt;setClientId('CLIENT-ID');
          $client-&gt;setClientSecret('CLIENT-SECRET');
          $client-&gt;setRedirectUri($url);
          $client-&gt;setScopes(array('https://www.googleapis.com/auth/drive'));
          $text = '';
  
          const DRIVE = "https://www.googleapis.com/auth/drive";
  
          if (isset($_GET['code'])) {
              $_SESSION['accessToken'] = $client-&gt;authenticate($_GET['code']);
              header('location:'.$url);exit;
          } elseif (!isset($_SESSION['accessToken'])) {
              $client-&gt;authenticate();
          }
          $files = array();
          $dir = dir('files');
          while ($file = $dir-&gt;read()) {
              if ($file != '.' &amp;&amp; $file != '..') {
                  $files[] = $file;
              }
          }
          $dir-&gt;close();
          if (!empty($_POST)) {
              $client-&gt;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-&gt;setTitle($file_name);
                  $file-&gt;setDescription('This is a .'.$mime_type.' document');
                  $file-&gt;setMimeType($mime_type);
                  $fileid = $file-&gt;getId();
  
                  $service-&gt;files-&gt;insert(
                      $file,
                      array(
                          'data' =&gt; file_get_contents($file_path),
                          'mimeType' =&gt; $mime_type
                      )
                  );
              }
              finfo_close($finfo);
              //First try
              $downloadUrl = $file-&gt;getDownloadUrl();
              header('location: '.$downloadUrl);exit;
  
          }
  
          function get($fileId, $optParams = array()) {
              $params = array('fileId' =&gt; $fileId);
              $params = array_merge($params, $optParams);
              $data = $this-&gt;__call('get', array($params));
              if ($this-&gt;useObjects()) {
                return new Google_DriveFile($data);
              } else {
                return $data;
              }
          }
  
  
          //Second try
          function downloadFile($service, $file) {
                $this-&gt;public;
                $downloadUrl = $file-&gt;getDownloadUrl();
                if ($downloadUrl) {
                  $request = new Google_Http_Request($downloadUrl, 'GET', null, null);
                  $httpRequest = $service-&gt;getClient()-&gt;getAuth()-&gt;authenticatedRequest($request);
                  if ($httpRequest-&gt;getResponseHttpCode() == 200) {
                    return $httpRequest-&gt;getResponseBody();
                  } else {
                    // An error occurred.
                    return null;
                  }
                } else {
                  // The file doesn't have any content stored on Drive.
                  return null;
                }
              }
  
          include 'index.phtml';
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>Made changes to the code following the example -  <a href="https://developers.google.com/drive/v3/reference/files/get#try-it" rel="nofollow noreferrer">https://developers.google.com/drive/v3/reference/files/get#try-it</a>, but error was appeared after I click submit button -  "Catchable fatal error: Object of class Google_DriveFile could not be converted to string".
  Updated:</p>
  
  <pre><code>&lt;?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-&gt;setClientId('xxx');
  $client-&gt;setClientSecret('xxx');
  $client-&gt;setRedirectUri($url);
  $client-&gt;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-&gt;authenticate($_GET['code']);
      header('location:'.$url);exit;
  } elseif (!isset($_SESSION['accessToken'])) {
      $client-&gt;authenticate();
  }
  $files = array();
  $dir = dir('files');
  while ($file = $dir-&gt;read()) {
      if ($file != '.' &amp;&amp; $file != '..') {
          $files[] = $file;
      }
  }
  $dir-&gt;close();
  if (!empty($_POST)) {
      $client-&gt;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-&gt;setTitle($file_name);
          $file-&gt;setDescription('This is a .'.$mime_type.' document');
          $file-&gt;setMimeType($mime_type);
          $fileid = $file-&gt;getId();
          $response = $service-&gt;$file-&gt;get($fileId, array(
            'alt' =&gt; 'media' ));
          $content = $response-&gt;getBody()-&gt;getContents();
          $service-&gt;files-&gt;insert(
              $file,
              array(
                  'data' =&gt; file_get_contents($file_path),
                  'mimeType' =&gt; $mime_type
              )
          );
      }
      finfo_close($finfo);
      header('location: '.$content);exit;
  
  }
  
  function get($fileId, $optParams = array()) {
      $params = array('fileId' =&gt; $fileId);
      $params = array_merge($params, $optParams);
      $data = $this-&gt;__call('get', array($params));
      if ($this-&gt;useObjects()) {
        return new Google_DriveFile($data);
      } else {
        return $data;
      }
  }
  
  include 'index.phtml';
  </code></pre>