Облицовочная ошибка "Неустранимая ошибка: исключить исключение" Google_Auth_Exception "с сообщением« Недопустимый код »в C: \ xampp"

Я сталкиваюсь с проблемой использования Google API для извлечения событий календаря. Ошибка, которую я получаю: –

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->authenticate('', false) #1 C:\xampp\htdocs\googlecale.php(38): Google_Client->authenticate('') #2 C:\xampp\htdocs\googlecale.php(71): getClient() #3 {main} thrown in C:\xampp\htdocs\vendor\Auth\OAuth2.php on line 88 

После расследования я обнаружил, что мой код не выполняется после $accessToken = $client->authenticate($authCode);
Я вставляю свой код ниже: –

 <?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->setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES); $client->setAuthConfigFile(CLIENT_SECRET_PATH); $client->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->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->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->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->refreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, $client->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' => 10, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c'), ); $results = $service->events->listEvents($calendarId, $optParams); if (count($results->getItems()) == 0) { print "No upcoming events found.\n"; } else { print "Upcoming events:\n"; foreach ($results->getItems() as $event) { $start = $event->start->dateTime; if (empty($start)) { $start = $event->start->date; } printf("%s (%s)\n", $event->getSummary(), $start); } } 

Пожалуйста, помогите мне выйти из этого спасибо 🙂 ниже мой скриншот ошибки: – введите описание изображения здесь

Ошибка в том, что вы не можете ввести токен, и вы запускаете код через веб-сервер. Вы должны запустить код из командной строки ( http://php.net/manual/en/features.commandline.usage.php )

Посмотрите на эту строку:

 $authCode = trim (fgets (STDIN)); 

Ошибка указала, что код авторизации недействителен, поскольку $ authCode является пустой строкой.