Method: users.watch
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Thiết lập hoặc cập nhật đồng hồ thông báo đẩy trên hộp thư của người dùng đã cho.
Yêu cầu HTTP
POST https://gmail.googleapis.com/gmail/v1/users/{userId}/watch
URL sử dụng cú pháp Chuyển mã gRPC.
Tham số đường dẫn
Thông số |
userId |
string
Địa chỉ email của người dùng. Bạn có thể sử dụng giá trị đặc biệt me để cho biết người dùng đã được xác thực.
|
Nội dung yêu cầu
Nội dung yêu cầu chứa dữ liệu có cấu trúc sau:
Trường |
labelIds[] |
string
Danh sách labelId để hạn chế thông báo. Theo mặc định, nếu bạn không chỉ định, tất cả các thay đổi sẽ được đẩy ra. Nếu được chỉ định, thì sẽ chỉ định những nhãn nào cần thiết để tạo thông báo đẩy.
|
labelFilterAction (deprecated) |
enum (LabelFilterAction )
Hành vi lọc của labelIds list được chỉ định. Trường này không còn được dùng nữa vì gây ra hành vi không chính xác trong một số trường hợp; hãy sử dụng labelFilterBehavior .
|
labelFilterBehavior |
enum (LabelFilterAction )
Hành vi lọc của labelIds list được chỉ định. Trường này thay thế labelFilterAction ; nếu được đặt, labelFilterAction sẽ bị bỏ qua.
|
topicName |
string
Tên chủ đề API Google Cloud Pub/Sub đủ điều kiện để phát hành sự kiện. Tên chủ đề này phải tồn tại trong Cloud Pub/Sub và bạn phải cấp cho Gmail quyền "xuất bản" trên chủ đề đó. Ví dụ: "projects/my-project-identifier/topics/my-topic-name" (sử dụng định dạng đặt tên chủ đề "v1" của Cloud Pub/Sub). Xin lưu ý rằng phần "my-project-identifier" phải khớp chính xác với mã dự án nhà phát triển của Google (mã thực thi yêu cầu xem này).
|
Nội dung phản hồi
Phản hồi thông báo đẩy trên đồng hồ.
Nếu thành công, phần nội dung phản hồi sẽ chứa dữ liệu có cấu trúc sau:
Biểu diễn dưới dạng JSON |
{
"historyId": string,
"expiration": string
} |
Trường |
historyId |
string
Mã của bản ghi nhật ký hiện tại của hộp thư.
|
expiration |
string (int64 format)
Thời điểm Gmail sẽ ngừng gửi thông báo về nội dung cập nhật hộp thư (theo mili giây theo hệ thống). Gọi lại watch trước thời điểm này để gia hạn đồng hồ.
|
Phạm vi uỷ quyền
Yêu cầu một trong các phạm vi OAuth sau:
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
Để biết thêm thông tin, hãy xem Hướng dẫn uỷ quyền.
LabelFilterAction
Hành vi lọc của danh sách labelIds được chỉ định.
Enum |
include |
Chỉ nhận thông báo đẩy cho các thay đổi về thông báo liên quan đến labelId được chỉ định. |
exclude |
Nhận thông báo đẩy về tất cả thay đổi đối với tin nhắn, ngoại trừ những thay đổi liên quan đến labelId được chỉ định. |
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-25 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-25 UTC."],[],[],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. |"]]