bscheshirwork / yii2-google-api-client-sa
为官方 Google API PHP Client 提供的 Yii2 包装器(如果使用 Service Account + DwD)
dev-master
2017-07-07 00:00 UTC
Requires
- google/apiclient: ^2.0
- yiisoft/yii2: ^2.0.12
This package is auto-updated.
Last update: 2024-09-08 07:47:43 UTC
README
为了轻松配置并提供服务访问
安装
首先,您可以在 DwD 中注册 Google 服务帐户,为该帐户指定用户,并启用此用户 ID 的 G suite 访问。请参阅 api-client-library service-accounts
其次 - 安装包装器并使用密钥数据配置它。
建议通过 composer 安装此扩展。
将以下内容
"bscheshirwork/yii2-google-api-client-sa": "*"
添加到您的 composer.json
文件的 require 部分。
此包还将安装 google/apiclient 库。
配置
您的应用程序可以添加到 Yii 配置数组的 components
索引中,使用所需数量的 Google_Service 实例。
以下是如何设置 GMail 的示例,以下提供了使用示例。
'components' => [ // .. 'gmail' => [ 'class' => 'bscheshirwork\gacsa\GoogleApiClient', 'googleApplicationCredentials' => '@runtime/secret-place/myprojectname-privatekeyshortdigits.json', 'api' => Google_Service_Gmail::class, ],
这将使您能够在应用程序中使用 GMail 认证的 Yii::$app->gmail->getService()
服务。
使用方法
显示 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>"; } } }
感谢 Mehdi Achour