Mario
Mario

Reputation: 49

Laravel Google Calendar API error

I'm trying to get Google Calendar API to work but im getting an error:

Class Google_Service_Calendar does not exist

App/Providers/GoogleClientServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class GoogleClientServiceProvider extends ServiceProvider {

    function register(){
        /* Get config variables */
        $service_account_name = getenv('GOOGLE_SERVICE_ACCOUNT_NAME');
        $key_file_location = base_path() . getenv('GOOGLE_KEY_FILE_LOCATION');

        $key = file_get_contents($key_file_location);
        /* Add the scopes you need */
        $scopes = ['https://www.googleapis.com/auth/calendar'];

        $cred = new \Google_Auth_AssertionCredentials(
            $service_account_name,
            $scopes,
            $key
        );

        $client = new \Google_Client();
        $client->setAssertionCredentials($cred);

        // repeat this block for as many Google_Services_* as you like
        // don't forget to add scopes for other services.
        $this->app->singleton(\Google_Service_Calendar::class, function ($app) use ($client) {
            return new \Google_Service_Calendar($client);
        });
    }
}

App/http/controllers/AgendaController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Google_Service_Calendar;
use App\kosten;
use App\User;

class AgendaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Google_Service_Calendar $client){
      $calendarList = $client->calendarList->listCalendarList();
      dd($calendarList->getItems());
    }  

    // public function index()
    // {


    //   return view('agenda');
    // }
}

The lines i added in .env

GOOGLE_SERVICE_ACCOUNT_NAME=GOOGLESERVICEACCOUNT
GOOGLE_KEY_FILE_LOCATION=/resources/assets/key.p12

Upvotes: 2

Views: 1298

Answers (1)

geckob
geckob

Reputation: 8130

As stated in the comment, I think you need to re-generate your autoload files.

Just run this:

composer dump-autoload

Upvotes: 1

Related Questions