Google API, Client and Services for Elgg
- Google JSON key dir:
elgg_get_data_path() . 'google/google_key.json';
How to get Google JSON credentials
- Google API Aliases
$classMap = [
'Google\\Client' => 'Google_Client',
'Google\\Service' => 'Google_Service',
'Google\\AccessToken\\Revoke' => 'Google_AccessToken_Revoke',
'Google\\AccessToken\\Verify' => 'Google_AccessToken_Verify',
'Google\\Model' => 'Google_Model',
'Google\\Utils\\UriTemplate' => 'Google_Utils_UriTemplate',
'Google\\AuthHandler\\Guzzle6AuthHandler' => 'Google_AuthHandler_Guzzle6AuthHandler',
'Google\\AuthHandler\\Guzzle7AuthHandler' => 'Google_AuthHandler_Guzzle7AuthHandler',
'Google\\AuthHandler\\Guzzle5AuthHandler' => 'Google_AuthHandler_Guzzle5AuthHandler',
'Google\\AuthHandler\\AuthHandlerFactory' => 'Google_AuthHandler_AuthHandlerFactory',
'Google\\Http\\Batch' => 'Google_Http_Batch',
'Google\\Http\\MediaFileUpload' => 'Google_Http_MediaFileUpload',
'Google\\Http\\REST' => 'Google_Http_REST',
'Google\\Task\\Retryable' => 'Google_Task_Retryable',
'Google\\Task\\Exception' => 'Google_Task_Exception',
'Google\\Task\\Runner' => 'Google_Task_Runner',
'Google\\Collection' => 'Google_Collection',
'Google\\Service\\Exception' => 'Google_Service_Exception',
'Google\\Service\\Resource' => 'Google_Service_Resource',
'Google\\Exception' => 'Google_Exception',
];
Add Google API key and import Google JSON credentials.
Now you can use Google services.
Here is an example for Google Service Sheets:
// Get the API client and construct the service object.
$api_key = elgg_get_plugin_setting('api_key','google_api');
// Initialize Google Client
$client = new \Google_Client();
$client->setDeveloperKey($api_key);
$client->setApplicationName('my_app');
$client->setScopes(\Google_Service_Sheets::SPREADSHEETS);
$client->setAccessType('offline');
// Authorize Google Client
$data = elgg_get_data_path() . 'google/google_key.json';
$client->setAuthConfig(json_decode($data, true));
// Initialize Google service
$service = new \Google_Service_Sheets($client);