title: Google API PHP Client - PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid code'
body: <p>I have uploaded my php application on Google App Engine. It is in the form of a scheduled task or a cron job. Can anyone help me out?</p>
  
  <p>I am getting the following error in logs:</p>
  
  <p>PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Invalid code' in /base/data/home/apps//google-api-php-client/src/Google/Client.php:168</p>
  
  <p>Here is my code:</p>
  
  <pre><code>function uploadData ($responseData){
  
  require __DIR__ . '/google-api-php-client/vendor/autoload.php';
  
  define('APPLICATION_NAME', 'Drive API PHP Quickstart');
  define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
  define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
  define('SCOPES', implode(' ', array(
          Google_Service_Drive::DRIVE_FILE)
  ));
  
  /**
   * Returns an authorized API client.
   * @return Google_Client the authorized client object
   */
  function getClient() {
      $client = new Google_Client();
      $client-&gt;setApplicationName(APPLICATION_NAME);
      $client-&gt;setScopes(SCOPES);
      $client-&gt;setAuthConfigFile(CLIENT_SECRET_PATH);
      $client-&gt;setAccessType('offline');
  
      // Load previously authorized credentials from a file.
      $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
      if (file_exists($credentialsPath)) {
          $accessToken = file_get_contents($credentialsPath);
      } 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;authenticate($authCode);
  
          // Store the credentials to disk.
          if(!file_exists(dirname($credentialsPath))) {
              mkdir(dirname($credentialsPath), 0700, true);
          }
          file_put_contents($credentialsPath, $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;refreshToken($client-&gt;getRefreshToken());
          file_put_contents($credentialsPath, $client-&gt;getAccessToken());
      }
      return $client;
  }
  
  /**
   * Expands the home directory alias '~' to the full path.
   * @param string $path the path to expand.
   * @return string the expanded path.
   */
  function expandHomeDirectory($path) {
      $homeDirectory = getenv('HOME');
      if (empty($homeDirectory)) {
          $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
      }
      return str_replace('~', realpath($homeDirectory), $path);
  }
  
  
  // Get the API client and construct the service object.
  $client = getClient();
  $service = new Google_Service_Drive($client);
  
  $title = 'ordersMonthly30Days';
  
  $file = new Google_Service_Drive_DriveFile($client);
  $file-&gt;setTitle($title);
  
  /*
  $result = $service-&gt;files-&gt;insert($file, array(
      'data' =&gt; $responseData,
      'mimeType' =&gt; 'application/octet-stream',
      'uploadType' =&gt; 'media',
      'convert' =&gt; true,
  ));*/
  
  $result = updateFile($service,'file id',$title,'testing update','application/octet-stream',$responseData,true);
  }
  
  /**
  * Update an existing file's metadata and content.
  *
  * @param Google_Service_Drive $service Drive API service instance.
  * @param string $fileId ID of the file to update.
  * @param string $newTitle New title for the file.
  * @param string $newDescription New description for the file.
  * @param string $newMimeType New MIME type for the file.
  * @param string $newFilename Filename of the new content to upload.
  * @param bool $newRevision Whether or not to create a new revision for this     file.
  * @return Google_Servie_Drive_DriveFile The updated file. NULL is returned   if
  *     an API error occurred.
  */
  function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
  try {
      // First retrieve the file from the API.
      $file = $service-&gt;files-&gt;get($fileId);
  
      // File's new metadata.
      $file-&gt;setTitle($newTitle);
      $file-&gt;setDescription($newDescription);
      $file-&gt;setMimeType($newMimeType);
  
      // File's new content.
      $data = $newFileName;
  
      $additionalParams = array(
          'newRevision' =&gt; $newRevision,
          'data' =&gt; $data,
          'mimeType' =&gt; $newMimeType
      );
  
      // Send the request to the API.
      $updatedFile = $service-&gt;files-&gt;update($fileId, $file, $additionalParams);
      return $updatedFile;
  } catch (Exception $e) {
      print "An error occurred: " . $e-&gt;getMessage();
  }
  }
  </code></pre>