PHP-Google API / Gmail API-如何设置quotaUser?

时间:2019-04-30 14:51:09

标签: php google-api gmail-api

我需要在我的Google API和Gmail API调用上强制执行每个用户的配额。

https://developers.google.com/gmail/api/v1/reference/query-parameters

如何使用PHP设置 quotaUser

$client = new Google_Client();
$client->setApplicationName('My App');
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setRedirectUri('my url');
$client->setScopes(
    array( Google_Service_Gmail::GMAIL_SEND,
           'email',
           'profile',
           'openid'
    )
);

$client->authenticate($_GET['code']);

$service = new Google_Service_Oauth2($client);
$info = $service->userinfo->get();

$gmail = new Google_Service_Gmail($client);
$gmail->users_messages->send('me', 'my message');

1 个答案:

答案 0 :(得分:0)

要强制执行每用户配额,您需要为“ quotaUser”字段分配一个唯一值(例如电子邮件),或将用户的IP分配给“ userIP”字段。 [1]

这是强制执行每个用户配额的有效代码示例(使用Gmail快速入门[2]在我的测试应用程序中为我工作,并添加了以下代码):

$optParams = array('quotaUser' => "username@gsuite-domain.com");
$response = $gmail->users_messages->send("me", $message, $optParams);

在Google Cloud平台控制台(console.cloud.google.com)中,您可以编辑每个用户和每个API的配额限制。阅读有关如何编辑配额限制的说明[3]。

您可以检查每种Gmail API方法使用的“配额单位”,这样您可以计算所需的配额天数限制。 [4]。

Gmail API [5]中的PHP UserMessages_Resource类文档。

[1] https://developers.google.com/gmail/api/v1/reference/query-parameters

[2] https://developers.google.com/gmail/api/quickstart/php

[3] https://cloud.google.com/apis/docs/capping-api-usage

[4] https://developers.google.com/gmail/api/v1/reference/quota

[5] https://developers.google.com/resources/api-libraries/documentation/gmail/v1/php/latest/class-Google_Service_Gmail_UsersMessages_Resource.html