管理配额
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google 日历 API 设有配额,以确保所有用户都能公平地使用该 API。
使用 Calendar API 时,需要考虑以下三项重要限制:
- API 使用配额是按项目和按用户强制执行的。如需了解详情,请参阅下一部分。
- 一般日历使用限制:避免超出日历使用限制。
- 操作限制:您随时可能会受到速率限制。例如,如果您尝试快速连续写入单个日历。
日历 API 用量配额类型
系统会强制执行两种类型的配额:
- 每分钟每个项目:这是您的 Google Cloud 项目发出的请求数。
- 每位用户每个项目每分钟的请求数:这是您的 Cloud 项目中任何一位特定用户发出的请求数。此限制旨在帮助您确保用户之间的使用量分配公平合理。
配额是使用滑动窗口按每分钟计算的,因此,如果在一分钟内流量快速突增并超出每分钟配额,则在下一个窗口期间会进行速率限制,以确保平均而言,您的使用量保持在配额范围内。
如果超出任一配额,系统会限制您的查询速率,并向您返回 403 usageLimits
状态代码或 429 usageLimits
状态代码。如果发生这种情况,您可以采取以下措施:
- 请务必遵循所有最佳实践:使用指数退避、随机化流量模式、使用推送通知。
- 如果您的项目不断发展壮大,用户数量也在增加,您可以申请提高每个项目的配额。
- 如果达到每位用户的配额限制,您可以执行以下操作:
- 如果您使用服务账号,请将负载分配给用户或在多个服务账号之间分摊负载。
- 虽然您可以申请提高单用户配额,但一般不建议将其提高到高于默认值,因为您的应用可能会开始达到其他类型的限制,例如一般日历使用限制或操作限制。
申请增加配额
如需查看或更改项目的用量限制,或申请增加配额,请按以下步骤操作:
- 如果您的项目还没有结算账号,请创建一个。
- 在 API 控制台中访问 API 库中的“已启用的 API”页面,然后从列表中选择一个 API。
- 如需查看和更改配额相关设置,请选择配额。如需查看用量统计信息,请选择使用量。
使用指数退避算法
如果我们希望您降低请求速率,会返回 403“usageLimits”响应或 429 响应(请参阅完整错误文档)。
这不是致命错误,我们希望您在短时间间隔后重试请求。如果请求的到达速度仍然过快,我们会再次询问,依此类推。为了使此方法正常运行,请求之间的延迟时间必须随时间增加。
通常,您应使用截断指数退避算法;Cloud Storage 文档很好地说明了此算法的工作原理和首选算法。如果您使用的是 Google 客户端库,系统通常会为您处理此问题;请参阅您的库文档。通常,您应该使用库实现,而不是自行编写。
随机化流量模式
日历客户端容易出现流量高峰,这是因为多个客户端同时执行操作。例如,日历客户端的一个常见不良做法是在午夜执行完全同步。这几乎肯定会导致您超出每分钟配额,并导致速率限制和退避。
为避免出现这种情况,请尽可能确保流量在全天均匀分布。如果您的客户端需要每天同步一次,请让客户端确定一个随机时间(每个客户端的时间都不同)。如果您需要定期执行某项操作,请将间隔时间调整为 +/- 25%。这样可以更均匀地分配流量,并提供更好的用户体验。
使用推送通知
一个常见的使用情形是,每当用户日历中的内容发生变化时,都希望执行某项操作。一种反模式是反复轮询每个感兴趣的日历。这样会很快用完您的所有配额。例如,如果您的应用有 5,000 名用户,并且每分钟轮询每位用户的日历一次,那么即使在完成任何工作之前,每分钟也至少需要 5,000 的配额。
服务器端应用可以注册推送通知,以便在发生感兴趣的事情时通知您。虽然设置这些功能需要更多工作,但可以大幅提高配额使用效率,并提供更好的用户体验。请务必指定您要接收通知的 eventType
。如需了解详情,请参阅推送通知。
使用服务账号进行适当的结算
如果您的应用使用全网域授权发出请求,默认情况下,服务账号会根据“每位用户每分钟每个项目”的配额收费,而不是您要模拟的用户。这意味着,即使服务账号可能在多个用户的日历上运行,也可能会用尽配额并受到速率限制。为避免出现此问题,您可以使用 quotaUser
网址参数(或 x-goog-quota-user
HTTP 标头)指明将向哪个用户收取费用。此值仅用于配额计算。如需了解详情,请参阅 Cloud 文档中的限制每位用户的请求数。
测试配额限制处理
为确保您的应用能够在实践中妥善处理达到配额限制的情况(例如,通过使用指数退避算法进行重试),并尽可能减少对用户的潜在干扰,我们强烈建议您在真实环境中测试此场景。
为了避免此类测试干扰您的实际应用使用,我们建议您在 Google API 控制台中注册一个仅用于测试的项目,并以类似于生产项目的方式对其进行配置。然后,您可以为该项目设置人为的低配额,并观察应用的运行情况。
价格
Google Calendar API 可完全免费使用。超出配额请求限制不会产生额外费用,也不会向您的账号收取费用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Manage quotas\n\nThe Google Calendar API has quotas to make sure that it's used fairly by all users.\nThere are three important limitations to consider when using the\nCalendar API:\n\n- **API usage quotas** are enforced per project and per user. See the next section for more information.\n- **General Calendar usage limits** : Avoid [Calendar use limits](https://support.google.com/a/answer/2905486).\n- **Operational limits:** you might be rate limited at any time. For example if you attempt to write to a single calendar in quick succession.\n\nTypes of Calendar API usage quotas\n----------------------------------\n\nTwo types of quotas are enforced:\n\n- **Per minute per project:** This is the number of requests made by your Google Cloud project.\n- **Per minute per project per user:** This is the number of requests made by any one particular user in your Cloud project. This limit aims at helping you ensure a fair distribution of usage among your users.\n\nQuotas are calculated per minute using a sliding window, so a rapid burst of\ntraffic which exceeds your per-minute quota during one minute will result in\nrate limiting during the next window to ensure that, on average, your usage\nremains within the quotas.\n\nIf either quota is exceeded, you are rate limited and receive a\n[`403 usageLimits` status code](https://developers.google.com/workspace/calendar/api/guides/errors#403_rate_limit_exceeded)\nor a\n[`429 usageLimits` status code](https://developers.google.com/workspace/calendar/api/guides/errors#429_too_many_requests)\nto your queries. If this happens, here's what you can do:\n\n1. Make sure you follow all the best practices: [use exponential backoff](#backoff), [randomize traffic patterns](#randomize_traffic_patterns), [use push notifications](#use_push_notifications).\n2. If your project is growing and you have more users, you can request an [increase in the per-project quota](#quota_increase).\n3. If the per-user quota limit is hit, you can do the following:\n - If you use a service account, [allocate the load to users](#proper_accounting_with_service_accounts) or split it between multiple service accounts.\n - While you can request an increase in the per-user quota, in general it is not recommended to increase it above the default value as your application may start to hit other types of limits, for example [general calendar use limits](https://support.google.com/a/answer/2905486), or operational limits.\n\nRequest for quota increase\n--------------------------\n\nTo view or change usage limits for your project, or to request an increase to\nyour quota, do the following:\n\n1. If you don't already have a [billing account](//cloud.google.com/billing/docs/how-to/manage-billing-account) for your project, then create one.\n2. [Visit the Enabled APIs page of the\n API library](https://console.cloud.google.com/apis/enabled) in the API Console, and select an API from the list.\n3. To view and change quota-related settings, select **Quotas** . To view usage statistics, select **Usage**.\n\nUse exponential backoff\n-----------------------\n\nWhen we want you to slow down your rate of requests, we will return a 403\n\"usageLimits\" response or a 429 response (see the\n[full error documentation](/workspace/calendar/api/guides/errors#403_calendar_usage_limits_exceeded)).\nThis is not a fatal error and we expect you to retry the request after a short\ninterval. If requests are still arriving too quickly, we will ask again, and\nso on. For this to work correctly, it is important that the delays between\nrequests increase over time.\n\nGenerally, you should use *truncated exponential backoff* ; the\n[Cloud Storage documentation](https://cloud.google.com/storage/docs/retry-strategy)\nhas a good explanation of how this works and the preferred algorithm. If you're\nusing a Google client library, this will normally be handled for you; consult\nyour library documentation. Normally, you should use the library implementation\nrather than write your own.\n\nRandomize traffic patterns\n--------------------------\n\nCalendar clients are prone to spiky traffic patterns caused by\nmultiple clients performing operations at the same time. For example, a common\nbad practice for a Calendar client is to perform a full sync at\nmidnight. This almost certainly would lead to exceeding your per-minute quota\nand result in rate limiting and backoffs.\n\nTo avoid this, make sure that your traffic is spread throughout the day\nwherever possible. If your client needs to do a daily sync, have the client\ndetermine a random time (different for each client). If you need to perform an\noperation on a regular basis, vary the interval +/- 25%. This will distribute\nthe traffic more evenly and provide a much better user experience.\n\nUse push notifications\n----------------------\n\nA common use case is to want to perform an action whenever something changes\nin the user's calendar. An anti-pattern here is to repeatedly poll every\ncalendar of interest. This will very quickly use up all your quota---for example,\nif your application has 5,000 users and polls each user's calendar once a\nminute, then this will require a per-minute quota of at least 5,000 even\nbefore any work is done.\n\nServer-side applications can register for push notifications, which allows us\nto notify you when something of interest happens. These require more work to\nset up, but allow for dramatically more efficient use of your quota, and\nprovide a better user experience. Make sure you specify the `eventType` for\nwhich you want to be notified. For more information, see\n[Push notifications](/workspace/calendar/v3/push).\n\nProper accounting with service accounts\n---------------------------------------\n\nIf your application is performing requests using\n[domain-wide delegation](/identity/protocols/oauth2/service-account#delegatingauthority),\nby default the *service account* is charged with regard to \"per minute per\nproject per user\" quotas, and not the user you're impersonating. This means\nthat the service account will likely run out of quota and be rate-limited,\neven though it might be operating on multiple users' calendars. You can avoid\nthis by using the `quotaUser` URL parameter (or `x-goog-quota-user` HTTP\nheader) to indicate which user will be charged. This is used only for quota\ncalculations. See\n[Limiting requests per user](https://cloud.google.com/apis/docs/capping-api-usage#limiting_requests_per_user)\nin the Cloud documentation for more information.\n\nTest quota limit handling\n-------------------------\n\nTo ensure that your application can gracefully handle reaching quota limits in\npractice (e.g. by [doing retries with exponential backoff](#backoff)) and to\nminimize any potential disturbances to your users we strongly recommend testing\nthis scenario out in a real environment.\n\nIn order for such a test not to interfere with your real application usage, we\nrecommend registering a separate test-only project in\n[Google API Console](https://console.cloud.google.com/) and\n[configuring it](https://developers.google.com/workspace/calendar/api/guides/auth)\nin a way similar to your production project. You can then\n[set](https://console.cloud.google.com/iam-admin/quotas) artificially low\nquotas for this project and observe the behavior of your application.\n\nPricing\n-------\n\n\nAll use of the Google Calendar API is available at no additional cost. Exceeding the quota\nrequest limits doesn't incur extra charges and your account is not billed."]]