面向错误“致命错误:在C:\xampp中带有消息”无效代码“的未捕获异常'Google_Auth_Exception' [英] Facing Error "Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in C:\xampp"

查看:197
本文介绍了面向错误“致命错误:在C:\xampp中带有消息”无效代码“的未捕获异常'Google_Auth_Exception'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Google API提取日历事件时遇到问题。
我得到的错误是: -

I am facing issue on using Google API for fetching Calendar Events. The Error i am getting is :-

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)之后执行;


我粘贴下面的代码:

After investigating i found my code is not executing after $accessToken = $client->authenticate($authCode);
I am Pasting my code below :-

<?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);
  }
}

请帮助我摆脱这种感谢:)
以下是我的错误截图: -

Please help me to get out of this thanks :) below is my error screenshot :-

推荐答案

您的错误是您无法输入令牌,并且您通过网络服务器运行代码。您必须从命令行运行代码( http://php.net/manual/ en / features.commandline.usage.php

The error that you have is that you can´t enter the token, and you ran the code through the web server. You  must run the code from command line (http://php.net/manual/en/features.commandline.usage.php)

看看这一行:

Look at this line:

$authCode = trim (fgets (STDIN));

该错误表示授权码无效,因为$ authCode是一个空字符串。

The error said that the authorization code is invalid as $authCode is an empty string.

这篇关于面向错误“致命错误:在C:\xampp中带有消息”无效代码“的未捕获异常'Google_Auth_Exception'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆