A Yii2 wrapper for the official Google API PHP Client (In case used Service Account + DwD)
For easily config and give you access to the service, (*1)
Installation
First you can register Google service account with DwD, user for this account and enable G suite access for this user id.
api-client-library service-accounts, (*2)
Second - install wrapper and configure it use secret data., (*3)
The preferred way to install this extension is through composer., (*4)
Add, (*5)
"bscheshirwork/yii2-google-api-client-sa": "*"
to the require section of your composer.json
file., (*6)
This package will also install the google/apiclient library., (*7)
Configuration
You application may use as much Google_Service instances as you need, by adding an entry into the components
index of the Yii configuration array., (*8)
Here's how to setup GMail for example, a usage sample is provided below., (*9)
'components' => [
// ..
'gmail' => [
'class' => 'bscheshirwork\gacsa\GoogleApiClient',
'googleApplicationCredentials' => '@runtime/secret-place/myprojectname-privatekeyshortdigits.json',
'api' => Google_Service_Gmail::class,
],
This will enable you to access the GMail authenticated service Yii::$app->gmail->getService()
in your application., (*10)
Usage
Displaying your newest message subject on GMail, (*11)
/**
* @var $service Google_Service_Gmail
*/
$service = Yii::$app->gmail->getService(function(){
$userToImpersonate = 'email@suitedomain.com';
/** @var \Google_Client $client */
$client->setSubject($userToImpersonate);
return $client;
});
$messages = $service->users_messages->listUsersMessages('me', [
'maxResults' => 1,
'labelIds' => 'INBOX',
]);
$list = $messages->getMessages();
if (count($list) == 0) {
echo "You have no emails in your INBOX .. how did you achieve that ??";
} else {
$messageId = $list[0]->getId(); // Grab first Message
$message = $service->users_messages->get('me', $messageId, ['format' => 'full']);
$messagePayload = $message->getPayload();
$headers = $messagePayload->getHeaders();
echo "Your last email subject is: ";
foreach ($headers as $header) {
if ($header->name == 'Subject') {
echo "<b>" . $header->value . "</b>";
}
}
}
Thanks Mehdi Achour, (*12)