【问题标题】:Google Calendar API, class Google_ServiceGoogle 日历 API,Google_Service 类
【发布时间】:2017-02-21 03:07:36
【问题描述】:

希望你能帮帮我。

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

为此,我遵循了以下步骤:

  • 通过 Google Developers 控制台注册应用
  • 使用 composer (google-api-php-client) 安装客户端库
  • 将脚本放在下面的供应商文件夹中:
require_once 'autoload.php'; 要求('google/apiclient-services/src/Google/Service/Oauth2.php'); session_start(); // ************************************************ ******** // // 从 https://console.developers.google.com 获取这些值 // 请务必启用 Analytics API // ************************************************ ******** // $client_id = 'myclientid'; $client_secret = '我的客户秘密'; $redirect_uri = 'https://domain.nl/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php'; //与谷歌控制台相同 $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->setAccessType('offline'); // 获取我们的刷新令牌 $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly')); //用于注销。 if (isset($_GET['logout'])) { 未设置($_SESSION['token']); } // 第 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); } 别的 { 休息; } } }


不幸的是,我在单击Authentication 的“接受”按钮后立即收到错误消息:

致命错误:在第 32 行的 /home/user/domains/domain.nl/private_html/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php 中找不到类“Google_Service”

Google research 到目前为止没有帮助。

可能的解决方案:

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

    require_once 'autoload.php';
  • 运行受支持的 PHP 版本。 检查(已尝试 5.6 + 7.1)。

  • 检查您在 composer.json 中的库与实际自动加载的库之间是否存在差异。 检查

感谢您的帮助,谢谢!

【问题讨论】:

    标签: php api calendar


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2014-09-29
      • 1970-01-01
      相关资源
      最近更新 更多