Skip to content

Instantly share code, notes, and snippets.

@WyattCast44
Created March 17, 2019 17:50
Show Gist options
  • Save WyattCast44/7d2d7e1f6bcd4b7e18f817cf8391db70 to your computer and use it in GitHub Desktop.
Save WyattCast44/7d2d7e1f6bcd4b7e18f817cf8391db70 to your computer and use it in GitHub Desktop.
<?php
namespace App\GSuite;
use Google_Client;
use Google_Service_Directory;
class GSuite
{
// TODO: To be continued...
/*
* Email address for admin user that should be used to perform API actions
* Needs to be created via Google Apps Admin interface and be added to an admin role
* that has permissions for Admin APIs for Users
*/
protected $delegatedAdmin = 'username@domain.com';
/*
* Some name you want to use for your app to report to Google with calls, I assume
* it is used in logging or something
*/
protected $appName = 'Your App Name';
/*
* Array of scopes you need for whatever actions you want to perform
* See https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
*/
protected $scopes = array(
'https://www.googleapis.com/auth/admin.directory.user'
);
/*
* Provide path to JSON credentials file that was downloaded from Google Developer Console
* for Service Account
*/
protected $credentialsFile;
protected $google_client = null;
protected $google_directory_client = null;
public function __construct()
{
$this->credentialsFile = 'path-to-creds.json';
}
protected function setGoogleClient()
{
$this->google_client = new Google_Client;
$this->google_client->setApplicationName($this->appName);
$this->google_client->setAuthConfig($this->credentialsFile);
$this->google_client->setSubject($this->delegatedAdmin);
$this->google_client->setScopes($this->scopes);
$this->google_client->setAccessType('offline');
}
protected function setGoogleDirectoryClient($google_client)
{
$this->google_directory_client = new Google_Service_Directory($google_client);
}
public function getGoogleClient()
{
if ($this->google_client === null) {
$this->setGoogleClient();
}
return $this->google_client;
}
public function getGoogleDirectoryClient()
{
if ($this->google_client === null) {
$this->setGoogleClient();
}
if ($this->google_directory_client === null) {
$this->setGoogleDirectoryClient($this->google_client);
}
return $this->google_directory_client;
}
public function test()
{
$client = $this->getGoogleDirectoryClient();
$user = $client->users->get('user-to-find@domain.com');
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment