gpt4 book ai didi

php - Google 日历 API,类 Google_Service

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:01 24 4
gpt4 key购买 nike

希望你能帮帮我。

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

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

  • 通过 Google Developers Console 注册应用
  • 使用 composer (google-api-php-client) 安装客户端库
  • 将脚本放在 vendor 文件夹中:
    require_once 'autoload.php';    require('google/apiclient-services/src/Google/Service/Oauth2.php');    session_start();     // ********************************************************  //    // Get these values from https://console.developers.google.com    // Be sure to enable the Analytics API    // ********************************************************    //    $client_id = 'myclientid';    $client_secret = 'myclientsecret';    $redirect_uri = 'https://domain.nl/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php'; //identical as in Google console    $client = new Google_Client();    $client->setApplicationName("Client_Library_Examples");    $client->setClientId($client_id);    $client->setClientSecret($client_secret);    $client->setRedirectUri($redirect_uri);    $client->setAccessType('offline');   // Gets us our refreshtoken    $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly'));    //For loging out.    if (isset($_GET['logout'])) {         unset($_SESSION['token']);    }    // Step 2: The user accepted your access now you need to exchange it.    if (isset($_GET['code'])) {    $client->authenticate($_GET['code']);      $_SESSION['token'] = $client->getAccessToken();    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));    }    // Step 1:  The user has not authenticated we give them a link to login        if (!isset($_SESSION['token'])) {    $authUrl = $client->createAuthUrl();    print "Connect Me!";    }        // Step 3: We have access we can now create our service    if (isset($_SESSION['token'])) {    $client->setAccessToken($_SESSION['token']);    print "LogOut
"; $service = new Google_Service_Calendar($client); $calendarList = $service->calendarList->listCalendarList();; while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary()."
\n"; // get events $events = $service->events->listEvents($calendarListEntry->id); foreach ($events->getItems() as $event) { echo "-----".$event->getSummary()."
"; } } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } }


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

Fatal error: Class 'Google_Service' not found in /home/user/domains/domain.nl/private_html/dev/vendor/google/apiclient-services/src/Google/Service/Oauth2.php on line 32

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

可能的解决方案:

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

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

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

感谢您的帮助,谢谢!

最佳答案

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

关于php - Google 日历 API,类 Google_Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com