2016-10-12 112 views
2

我希望你能帮助我。Google Calendar API,Google_Service类

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

为此,我已按照下列步骤操作:

  • 通过谷歌开发者控制台注册应用
  • 使用作曲家(谷歌API的PHP客户端)
  • 在供应商下面放置脚本安装的客户端库文件夹:
 

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


的Un幸运的是,我点击“接受”按钮为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中的库和实际正在自动加载的库之间是否有不同。 检查

您的帮助表示感谢,谢谢!

回答

0

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