bscheshirwork/yii2-google-api-client-sa

A Yii2 wrapper for the official Google API PHP Client (In case used Service Account + DwD)

dev-master 2017-07-07 00:00 UTC

This package is auto-updated.

Last update: 2024-04-08 06:56:21 UTC


README

For easily config and give you access to the service

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

Second - install wrapper and configure it use secret data.

The preferred way to install this extension is through composer.

Add

"bscheshirwork/yii2-google-api-client-sa": "*"

to the require section of your composer.json file.

This package will also install the google/apiclient library.

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.

Here's how to setup GMail for example, a usage sample is provided below.

    '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.

Usage

Displaying your newest message subject on GMail

/**
 * @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