我希望你能帮助我。
我正在尝试使用Oauth2身份验证连接到 Google(日历)API 。
为此,我遵循了以下步骤:
require_once 'autoload.php';
需要('google/apiclient-services/src/Google/Service/Oauth2.php');
会话开始();
// ************************************************** ****** //
// 从 https://console.developers.google.com 获取这些值
// 确保启用 Analytics API
// ************************************************** ****** //
$client_id = 'myclientid';
$client_secret = 'myclientsecret';
$redirect_uri = 'https://domain.nl/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php'; //与Google控制台中的相同
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('离线'); // 获取我们的刷新令牌
$client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly'));
//用于注销。
if (isset($_GET['注销'])) {
取消设置($_SESSION['令牌']);
}
// 步骤 2:用户接受了您的访问权限,现在您需要交换它。
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'https://' 。$_SERVER['HTTP_HOST'] 。$_SERVER['PHP_SELF'];
header('位置:' .filter_var($redirect, FILTER_SANITIZE_URL));
}
// 第 1 步:用户尚未经过身份验证,我们为他们提供登录链接
if (!isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
打印“连接我!”;
}
// 第 3 步:我们拥有访问权限,现在可以创建我们的服务
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
打印“退出
”;
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();;
而(真){
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary()."
\n";
// 获取事件
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "-----".$event->getSummary()."
";
}
}
$pageToken = $calendarList->getNextPageToken();
如果($pageToken){
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} 别的 {
休息;
}
}
}
不幸的是,我在单击身份验证
的“接受”按钮后立即收到错误:
致命错误:在第 32 行 /home/user/domains/domain.nl/private_html/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php 中找不到类“Google_Service”
谷歌研究到目前为止没有帮助。
可能的解决方案:
正确设置自动加载路径。查看。
require_once 'autoload.php';
运行受支持的 PHP 版本。检查(尝试过 5.6 + 7.1)。
感谢您的帮助,谢谢!