title: Facing Error "Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in C:\xampp"
body: <p>I am facing issue on using Google API for fetching Calendar Events.
  The Error i am getting is :-</p>
  
  <pre><code>Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in C:\xampp\htdocs\vendor\Auth\OAuth2.php:88 Stack trace: #0 C:\xampp\htdocs\vendor\Client.php(128): Google_Auth_OAuth2-&gt;authenticate('', false) #1 C:\xampp\htdocs\googlecale.php(38): Google_Client-&gt;authenticate('') #2 C:\xampp\htdocs\googlecale.php(71): getClient() #3 {main} thrown in C:\xampp\htdocs\vendor\Auth\OAuth2.php on line 88
  </code></pre>
  
  <p>After investigating i found my code is not executing after 
  <code>$accessToken = $client-&gt;authenticate($authCode);</code><br>
  I am Pasting my code below :-</p>
  
  <pre><code>&lt;?php
  require 'vendor/autoload.php';
  define('STDIN',fopen("php://stdin","r"));
  define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
  define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json');
  define('CLIENT_SECRET_PATH', 'client_secret.json');
  define('SCOPES', implode(' ', array(
    Google_Service_Calendar::CALENDAR_READONLY)
  ));
  
  /**
   * 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);
      //var_dump('hi');exit;
      // 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_Calendar($client);
  
  // Print the next 10 events on the user's calendar.
  $calendarId = 'primary';
  $optParams = array(
    'maxResults' =&gt; 10,
    'orderBy' =&gt; 'startTime',
    'singleEvents' =&gt; TRUE,
    'timeMin' =&gt; date('c'),
  );
  $results = $service-&gt;events-&gt;listEvents($calendarId, $optParams);
  
  if (count($results-&gt;getItems()) == 0) {
    print "No upcoming events found.\n";
  } else {
    print "Upcoming events:\n";
    foreach ($results-&gt;getItems() as $event) {
      $start = $event-&gt;start-&gt;dateTime;
      if (empty($start)) {
        $start = $event-&gt;start-&gt;date;
      }
      printf("%s (%s)\n", $event-&gt;getSummary(), $start);
    }
  }
  </code></pre>
  
  <p>Please help me to get out of this thanks :)
  below is my error screenshot :-
  <a href="https://i.stack.imgur.com/vG36g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vG36g.png" alt="enter image description here"></a></p>