title: Google Drive PHP API - File Upload
body: <p>I am trying to upload file in google drive using google drive api in php.
  I am getting Files name and Id from google drive correctly.But It could not uploding files to google drive and show some error. I could not identify if some client library package is missing or something else happens in it </p>
  
  <pre><code>Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission: Request had insufficient authentication scopes." } ], "code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes." } } in C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google\Http\REST.php:118 Stack trace: #0 C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google\Task\Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google\Http\REST.php(58): Google_Task_Runner-&gt;run() #3 C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google in C:\xampp\htdocs\googledrive\vendor\google\apiclient\src\Google\Http\REST.php on line 118
  </code></pre>
  
  <p>My Source Code is </p>
  
  <pre><code>&lt;?php
  require __DIR__ . '/vendor/autoload.php';
  
  
  
  /**
   * Returns an authorized API client.
   * @return Google_Client the authorized client object
   */
  function getClient()
  {
      $client = new Google_Client();
      $client-&gt;setApplicationName('Google Drive API PHP Quickstart');
      $client-&gt;setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
      $client-&gt;setAuthConfig('credentials.json');
      $client-&gt;setAccessType('offline');
      $client-&gt;setPrompt('select_account consent');
  
      // Load previously authorized token from a file, if it exists.
      // The file token.json stores the user's access and refresh tokens, and is
      // created automatically when the authorization flow completes for the first
      // time.
      $tokenPath = 'token.json';
      if (file_exists($tokenPath)) {
          $accessToken = json_decode(file_get_contents($tokenPath), true);
          $client-&gt;setAccessToken($accessToken);
      }
  
      // If there is no previous token or it's expired.
      if ($client-&gt;isAccessTokenExpired()) {
          // Refresh the token if possible, else fetch a new one.
          if ($client-&gt;getRefreshToken()) {
              $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
          } else {
              // Request authorization from the user.
              $authUrl = $client-&gt;createAuthUrl();
              printf("Open the following link in your browser:\n%s\n", $authUrl);
              print 'Enter verification code: ';
              $authCode = trim(fgets(STDIN));
  
              // Exchange authorization code for an access token.
              $accessToken = $client-&gt;fetchAccessTokenWithAuthCode($authCode);
              $client-&gt;setAccessToken($accessToken);
  
              // Check to see if there was an error.
              if (array_key_exists('error', $accessToken)) {
                  throw new Exception(join(', ', $accessToken));
              }
          }
          // Save the token to a file.
          if (!file_exists(dirname($tokenPath))) {
              mkdir(dirname($tokenPath), 0700, true);
          }
          file_put_contents($tokenPath, json_encode($client-&gt;getAccessToken()));
      }
      return $client;
  }
  
  
  // Get the API client and construct the service object.
  $client = getClient();
  $service = new Google_Service_Drive($client);
  
  // Print the names and IDs for up to 10 files.
  $optParams = array(
    'pageSize' =&gt; 10,
    'fields' =&gt; 'nextPageToken, files(id, name)'
  );
  $results = $service-&gt;files-&gt;listFiles($optParams);
  
  if (count($results-&gt;getFiles()) == 0) {
      print "No files found.\n";
  } else {
      print "Files:\n";
      foreach ($results-&gt;getFiles() as $file) {
          printf("%s (%s)\n&lt;/br&gt;", $file-&gt;getName(), $file-&gt;getId());
      }
  }
  
  
  
  $pasta      = 'img';   
  $arquivo    = 'photo.jpg'; // file.mp4
      printf("Folder: %s || File: %s\n", $pasta, $arquivo);
  
  
  $parentId   = 'replace with Folder ID';
  
  
  if ( !empty($arquivo) ) {
  
      //Define localização da pasta e arquivo
      $file_path = $pasta.'/'.$arquivo;
  
      //Conecta no Drive da sua conta
      $file = new Google_Service_Drive_DriveFile();
  
      //Define nome do arquivo
      $file-&gt;setName($arquivo);
  
      //Define Diretório Destino lá no Google Drive
      $file-&gt;setParents(array($parentId));
  
      //Cria o arquivo no GDrive
      $service-&gt;files-&gt;create(
        $file,
        array(
          'data'          =&gt; file_get_contents($file_path),
          'mimeType'      =&gt; 'application/octet-stream',
          'uploadType' =&gt; 'resumable'
        )
      );
  
      // Grava Log do que foi feito UTC -3 horas;
      $DateTime = new DateTime();
      $DateTime-&gt;modify('-3 hours');
      $now = $DateTime-&gt;format("Y-m-d H:i:s");
      $logfile = $now.' Upload OK :: '.$arquivo.PHP_EOL;
  
      $myfile = file_put_contents('logs.txt', $logfile, FILE_APPEND | LOCK_EX);
  
  } else {
  
      //Grava Log
      $now = date("Y-m-d H:i:s");
      $logfile .=$now.' =====WITHOUT FILES========'.PHP_EOL;
  
      $myfile = file_put_contents('logs.txt', $logfile, FILE_APPEND | LOCK_EX);
  
  }
  
  
  </code></pre>
  
  <p>Please help me, What is the problem behind it.</p>