Google Calendar API, class Google_Service


Google Calendar API, class Google_Service

我希望你能帮助我。

我正在尝试使用Oauth2身份验证连接到Google (Calendar) API。

我遵循了以下步骤:

  • 通过Google开发者控制台注册应用
  • 使用composer (google-api-php-client)安装客户端库
  • 将脚本放置在供应商文件夹下:
<>之前require_once autoload.php";要求(谷歌/apiclient-services/src/谷歌/服务/Oauth2.php ');session_start ();//******************************************************** ////从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();客户端-> setApplicationName("Client_Library_Examples");客户端-> setClientId ($ client_id);客户端-> setClientSecret ($ client_secret);客户端-> setRedirectUri ($ redirect_uri);客户端-> setAccessType("网下");//获取我们的refreshtoken客户端-> setScopes(数组("https://www.googleapis.com/auth/calendar.readonly"));//注销。if (isset($_GET['logout'])) {设置($ _SESSION("令牌"));}//步骤2:用户接受你的访问,现在你需要交换它。if (isset($_GET['code'])) {客户端->验证美元($ _GET['代码']);$_SESSION['token'] = $client->getAccessToken();$redirect = 'https://' . '$ _SERVER [' HTTP_HOST ']。$ _SERVER [' PHP_SELF '];header('Location: ')。使用filter_var(重定向,FILTER_SANITIZE_URL));}//步骤1:用户还没有通过身份验证,我们给他们一个登录链接if (!isset($_SESSION['token'])) {$authUrl = $client->createAuthUrl();打印"连接我!";}//步骤3:我们有了访问权限,现在可以创建我们的服务了if (isset($_SESSION['token'])) {客户端-> setAccessToken ($ _SESSION("令牌"));打印"注销
";$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-> getiitems()作为$event) {回声 "-----".$ 事件——> getSummary()"。
";}}$pageToken = $calendarList->getNextPageToken();if ($pageToken) {$optParams = array('pageToken' => $pageToken);$calendarList = $service->calendarList->listCalendarList($optParams);} else {打破;}}}之前


不幸的是,在单击身份验证的"接受"按钮后,我得到了一个错误:

<>之前致命错误:在/home/user/domain/domain.nl/private_html/dev/vendor/google/apicclient -services/src/google/Service/Oauth2.php第32行中没有找到"Google_Service"类之前到目前为止,谷歌研究没有帮助。

可能的解决方案:

  • 正确设置自动加载路径。

    <>之前require_once autoload.php";
  • 运行支持的PHP版本。检查(已尝试5.6 + 7.1).

  • 检查您在composer中的库之间是否有不同之处。Json和正在自动加载的库。

感谢您的帮助!

更新:这已被修复。我使用Oauth2.php作为我的redirect_url,而不是返回到我的脚本。初学者错误:)