PHP code example of redbaron76 / googlavel

1. Go to this page and download the library: Download redbaron76/googlavel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redbaron76 / googlavel example snippets



// app/config/app.php

'providers' => array(
    '...',
    'Redbaron76\Googlavel\GooglavelServiceProvider'
);


// app/config/packages/redbaron76/googlavel/config.php

return array(

    // OAuth2 Setting, you can get these keys in Google Developers Console
    'oauth2_client_id'      => '< YOUR CLIENT ID >',
    'oauth2_client_secret'  => '< YOUR CLIENT SECRET >',
    'oauth2_redirect_uri'   => 'http://localhost:8081/',   // Change it according to your needs

    ...
  );


// routes.php

Route::get('/', function()
{
	if ( Input::has('code') )
	{
		$code = Input::get('code');
		
		// authenticate with Google API
		if ( Googlavel::authenticate($code) )
		{
			return Redirect::to('/protected');
		}
	}
	
	// get auth url
	$url = Googlavel::authUrl();
	
	return link_to($url, 'Login with Google!');
});

Route::get('/logout', function()
{
	// perform a logout with redirect
	return Googlavel::logout('/');
});

Route::get('/protected', function()
{
	// Get the google service (related scope must be set)
	$service = Googlavel::getService('Calendar');
	
	// invoke API call
	$calendarList = $service->calendarList->listCalendarList();

	foreach ( $calendarList as $calendar )
	{
		echo "{$calendar->summary} <br>";
	}

	return link_to('/logout', 'Logout');
});