title: Google Drive PHP API: Delete shows Insufficient Permission
body: <p>I've been working on deleting a file in my Google Drive but I can't make it work. I have changed my scope from <code>$client-&gt;setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);</code> to <code>$client-&gt;setScopes(Google_Service_Drive::DRIVE);</code>.</p>
  
  <p>Here's my full code:</p>
  
  <pre><code>&lt;?php
  require __DIR__ . '/vendor/autoload.php';
  
  if (php_sapi_name() != 'cli') {
   throw new Exception('This application must be run on the command line.');
  }
  
  /**
  * 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;setScopes(Google_Service_Drive::DRIVE);
    //$client-&gt;setScopes(Google_Service_Drive::DRIVE_APPDATA);
    //$client-&gt;setScopes(Google_Service_Drive::DRIVE_FILE);
    //$client-&gt;setAuthConfig('credentials.json');
    $client-&gt;setAccessType('offline');
  
    // Load previously authorized credentials from a file.
    $credentialsPath = 'token.json';
    if (file_exists($credentialsPath)) {
      $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } 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);
  
      // Check to see if there was an error.
      if (array_key_exists('error', $accessToken)) {
          throw new Exception(join(', ', $accessToken));
      }
  
      // Store the credentials to disk.
      if (!file_exists(dirname($credentialsPath))) {
          mkdir(dirname($credentialsPath), 0700, true);
      }
      file_put_contents($credentialsPath, json_encode($accessToken));
      printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client-&gt;setAccessToken($accessToken);
  
  // Refresh the token if it's expired.
  if ($client-&gt;isAccessTokenExpired()) {
      $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
      file_put_contents($credentialsPath, json_encode($client-&gt;getAccessToken()));
  }
      return $client;
  }
  
  
  // Get the API client and construct the service object.
  $client = getClient();
  $service = new Google_Service_Drive($client);
  
  /*Get Files under hourly_backup*/
  $hourly_backup_id = '1Y3cGEwXy9gLcw9WO0isqwtwtysU0g_bK';
  
  $optParams = array(
    //'pageSize' =&gt; 20,
    'fields' =&gt; 'nextPageToken, files(id,name,size,parents,createdTime)',
    'q' =&gt; "'".$hourly_backup_id."' in parents"
  );
  $results = $service-&gt;files-&gt;listFiles($optParams);
  
  if (count($results-&gt;getFiles()) == 0) {
      print "No files found.\n";
  } else {
      foreach ($results-&gt;getFiles() as $file) {
          if(strtotime(date('Y-m-d H:i:s', strtotime($file-&gt;getcreatedTime()))) &lt;= strtotime('-48 hours')){
              try {
                  return $service-&gt;files-&gt;delete($file-&gt;getId());
              } catch (Exception $e) {
                  print "An error occurred: " . $e-&gt;getMessage();
              }
              return NULL;
          } else {
             print "weh \n";
          }
      }
  }
  </code></pre>
  
  <p>If the condition is met and the delete code is ran, this is what I get:</p>
  
  <pre><code>An error occurred: {
   "error": {
     "errors": [
     {
      "domain": "global",
      "reason": "insufficientPermissions",
      "message": "Insufficient Permission"
     }
    ],
    "code": 403,
    "message": "Insufficient Permission"
   }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I also tried in the API Explorer and it worked without any problem.</p>
  
  <p>The google drive I'm working on is mine and I am able to delete a file directly without any problems. What could be missing in my code?</p>