Method: users.watch
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
HTTP 要求
POST https://gmail.googleapis.com/gmail/v1/users/{userId}/watch
這個網址使用 gRPC 轉碼語法。
路徑參數
參數 |
userId |
string
使用者的電子郵件地址。特殊值 me 可用於指出已驗證的使用者。
|
要求主體
要求主體的資料會採用以下結構:
欄位 |
labelIds[] |
string
要限制通知的 labelId 清單。根據預設,如果未指定,系統會推送所有變更。如果指定此屬性,則系統會根據所需的標籤產生推播通知。
|
labelFilterAction (deprecated) |
enum (LabelFilterAction )
指定 labelIds list 的篩選行為。這個欄位已淘汰,因為在某些情況下會導致不正確的行為;請改用 labelFilterBehavior 。
|
labelFilterBehavior |
enum (LabelFilterAction )
指定 labelIds list 的篩選行為。這個欄位會取代 labelFilterAction ;如果已設定,系統會忽略 labelFilterAction 。
|
topicName |
string
要發布事件的完整 Google Cloud Pub/Sub API 主題名稱。這個主題名稱「必須」已在 Cloud Pub/Sub 中存在,且您「必須」已授予 Gmail「發布」權限。例如「projects/my-project-identifier/topics/my-topic-name」(使用 Cloud Pub/Sub「v1」主題命名格式)。 請注意,「my-project-identifier」部分必須與您的 Google 開發人員專案 ID (執行此監控要求的 ID) 完全相符。
|
回應主體
推播通知手錶回應。
如果成功,回應主體會含有以下結構的資料:
JSON 表示法 |
{
"historyId": string,
"expiration": string
} |
欄位 |
historyId |
string
信箱目前記錄的歷史記錄 ID。
|
expiration |
string (int64 format)
Gmail 停止傳送信箱更新通知的時間 (紀元紀元前毫秒)。請在期限前再次呼叫 watch ,以便續約手錶。
|
授權範圍
需要下列其中一種 OAuth 範圍:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.metadata
詳情請參閱授權指南。
LabelFilterAction
列舉 |
include |
只針對與指定 labelId 相關的訊息變更接收推播通知。 |
exclude |
接收所有訊息異動推播通知,但不包括與指定 labelId 相關的異動。 |
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["# Method: users.watch\n\n- [HTTP request](#body.HTTP_TEMPLATE)\n- [Path parameters](#body.PATH_PARAMETERS)\n- [Request body](#body.request_body)\n - [JSON representation](#body.request_body.SCHEMA_REPRESENTATION)\n- [Response body](#body.response_body)\n - [JSON representation](#body.WatchResponse.SCHEMA_REPRESENTATION)\n- [Authorization scopes](#body.aspect)\n- [LabelFilterAction](#LabelFilterAction)\n\nSet up or update a push notification watch on the given user mailbox.\n\n### HTTP request\n\n`POST https://gmail.googleapis.com/gmail/v1/users/{userId}/watch`\n\nThe URL uses [gRPC Transcoding](https://google.aip.dev/127) syntax.\n\n### Path parameters\n\n| Parameters ||\n|----------|-----------------------------------------------------------------------------------------------------------|\n| `userId` | `string` The user's email address. The special value `me` can be used to indicate the authenticated user. |\n\n### Request body\n\nThe request body contains data with the following structure:\n\n| JSON representation |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ``` { \"labelIds\": [ string ], \"labelFilterAction\": enum (/workspace/gmail/api/reference/rest/v1/users/watch#LabelFilterAction), \"labelFilterBehavior\": enum (/workspace/gmail/api/reference/rest/v1/users/watch#LabelFilterAction), \"topicName\": string } ``` |\n\n| Fields ||\n|--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `labelIds[]` | `string` List of labelIds to restrict notifications about. By default, if unspecified, all changes are pushed out. If specified then dictates which labels are required for a push notification to be generated. |\n| `labelFilterAction` **(deprecated)** | `enum (`[LabelFilterAction](/workspace/gmail/api/reference/rest/v1/users/watch#LabelFilterAction)`)` Filtering behavior of `labelIds list` specified. This field is deprecated because it caused incorrect behavior in some cases; use `labelFilterBehavior` instead. |\n| `labelFilterBehavior` | `enum (`[LabelFilterAction](/workspace/gmail/api/reference/rest/v1/users/watch#LabelFilterAction)`)` Filtering behavior of `labelIds list` specified. This field replaces `labelFilterAction`; if set, `labelFilterAction` is ignored. |\n| `topicName` | `string` A fully qualified Google Cloud Pub/Sub API topic name to publish the events to. This topic name **must** already exist in Cloud Pub/Sub and you **must** have already granted gmail \"publish\" permission on it. For example, \"projects/my-project-identifier/topics/my-topic-name\" (using the Cloud Pub/Sub \"v1\" topic naming format). Note that the \"my-project-identifier\" portion must exactly match your Google developer project id (the one executing this watch request). |\n\n### Response body\n\nPush notification watch response.\n\nIf successful, the response body contains data with the following structure:\n\n| JSON representation |\n|-------------------------------------------------------|\n| ``` { \"historyId\": string, \"expiration\": string } ``` |\n\n| Fields ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `historyId` | `string` The ID of the mailbox's current history record. |\n| `expiration` | `string (`[int64](https://developers.google.com/discovery/v1/type-format)` format)` When Gmail will stop sending notifications for mailbox updates (epoch millis). Call `watch` again before this time to renew the watch. |\n\n### Authorization scopes\n\nRequires one of the following OAuth scopes:\n\n- `https://mail.google.com/`\n- `\n https://www.googleapis.com/auth/gmail.modify`\n- `\n https://www.googleapis.com/auth/gmail.readonly`\n- `\n https://www.googleapis.com/auth/gmail.metadata`\n\nFor more information, see the [Authorization guide](/workspace/guides/configure-oauth-consent).\n\nLabelFilterAction\n-----------------\n\nFiltering behavior of labelIds list specified.\n\n| Enums ||\n|-----------|---------------------------------------------------------------------------------------------|\n| `include` | Only get push notifications for message changes relating to labelIds specified. |\n| `exclude` | Get push notifications for all message changes except those relating to labelIds specified. |"]]