Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Calendar API, class Google_Service

Tags:

php

calendar

api

I hope you can help me out.

I am trying to connect to the Google (Calendar) API using Oauth2 authentication.

For this I have followed these steps:

  • Registering App through Google Developers console
  • Installed client library using composer (google-api-php-client)
  • Placed script below in the vendor folder:

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


Unfortunately I am getting an error right after clicking the "accept" button for the 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 did not help so far.

Possible solutions:

  • Set autoload path correctly. Check.

    require_once 'autoload.php';
    
  • Run a supported PHP version. Check (tried 5.6 + 7.1).

  • Check if there is something different between the library you in composer.json and the library that is actually being auto-loaded. Check.

Your help is appreciated, thanks!

like image 940
Steve Avatar asked Nov 08 '22 07:11

Steve


1 Answers

Update: this has been fixed. I used Oauth2.php as my redirect_url instead of returning to my script. Beginners mistake :)

like image 149
Steve Avatar answered Nov 15 '22 12:11

Steve