@cope99 thank you so much, i was hoping to find this
Google API - Service account connection - Laravel 5
For anyone who would like to use Google API with a service account connection, here is what I have to done in order to make it work :
1- Call the following in command line : composer require google/apiclient
2- In your composer.json file add the following :
{
"autoload": {
"classmap": [
"vendor/google/apiclient/src/Google"
],
}
}
3- In your http://console.developers.google.com Create a new Client ID in Credentials, Select Service account and click on Create Client ID
4- Next, take the *.p12 file and place it somewhere in your laravel directory (in my case in assets).
5- Next, create a new Gooogle calendar (http://calendar.google.com) with any Google account and share it with your Service Account's Email Address created in step 3 and save it.
6- In the config folder, add a file name google.php, place and modify the following content :
<?php
return [
/*
|--------------------------------------------------------------------------
| Client ID
|--------------------------------------------------------------------------
|
| The Client ID can be found in the OAuth Credentials under Service Account
|
*/
'client_id' => 'something.apps.googleusercontent.com',
/*
|--------------------------------------------------------------------------
| Service account name
|--------------------------------------------------------------------------
|
| The Service account name is the Email Address that can be found in the
| OAuth Credentials under Service Account
|
*/
'service_account_name' => 'something@developer.gserviceaccount.com',
/*
|--------------------------------------------------------------------------
| Key file location
|--------------------------------------------------------------------------
|
| This is the location of the .p12 file from the Laravel root directory
|
*/
'key_file_location' => '/resources/assets/filename.p12',
];
7- In your application folder, I have added a file named GoogleCalendar.php in App\Services with the following content :
<?php namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
class GoogleCalendar {
protected $client;
protected $service;
function __construct() {
/* Get config variables */
$client_id = Config::get('google.client_id');
$service_account_name = Config::get('google.service_account_name');
$key_file_location = base_path() . Config::get('google.key_file_location');
$this->client = new \Google_Client();
$this->client->setApplicationName("Your Application Name");
$this->service = new \Google_Service_Calendar($this->client);
/* If we have an access token */
if (Cache::has('service_token')) {
$this->client->setAccessToken(Cache::get('service_token'));
}
$key = file_get_contents($key_file_location);
/* Add the scopes you need */
$scopes = array('https://www.googleapis.com/auth/calendar');
$cred = new \Google_Auth_AssertionCredentials(
$service_account_name,
$scopes,
$key
);
$this->client->setAssertionCredentials($cred);
if ($this->client->getAuth()->isAccessTokenExpired()) {
$this->client->getAuth()->refreshTokenWithAssertion($cred);
}
Cache::forever('service_token', $this->client->getAccessToken());
}
public function get($calendarId)
{
$results = $this->service->calendars->get($calendarId);
dd($results);
}
}
8- Next add the following where you need to use Google Calendar
use App\Services\GoogleCalendar;
[...]
public function functionName(GoogleCalendar $calendar)
{
$calendarId = "YourCalendarID";
$result = $calendar->get($calendarId);
}
Please or to participate in this conversation.
