【问题标题】:PHP - Google API / Gmail API - How to set quotaUser?PHP - Google API / Gmail API - 如何设置quotaUser?
【发布时间】:2019-09-19 06:28:48
【问题描述】:

我需要对我的 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');

【问题讨论】:

    标签: php google-api gmail-api


    【解决方案1】:

    要强制执行每个用户的配额,您需要为“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

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多