Method: users.watch
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตั้งค่าหรืออัปเดตการตรวจสอบการแจ้งเตือนแบบพุชในกล่องจดหมายของผู้ใช้ที่ระบุ
คำขอ HTTP
POST https://gmail.googleapis.com/gmail/v1/users/{userId}/watch
URL ใช้ไวยากรณ์การแปลง gRPC
พารามิเตอร์เส้นทาง
พารามิเตอร์ |
userId |
string
อีเมลของผู้ใช้ ค่าพิเศษ me ใช้เพื่อระบุผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์ได้
|
เนื้อหาของคำขอ
เนื้อความของคำขอมีข้อมูลซึ่งมีโครงสร้างดังต่อไปนี้
ช่อง |
labelIds[] |
string
รายการ labelId เพื่อจํากัดการแจ้งเตือน โดยค่าเริ่มต้น ระบบจะพุชการเปลี่ยนแปลงทั้งหมดหากไม่ได้ระบุ หากระบุไว้ ระบบจะกำหนดป้ายกำกับที่จำเป็นในการสร้างข้อความ Push
|
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" (ใช้รูปแบบการตั้งชื่อหัวข้อ "v1" ของ Cloud Pub/Sub) โปรดทราบว่าส่วน "my-project-identifier" ต้องตรงกับรหัสโปรเจ็กต์นักพัฒนาแอป Google ของคุณทุกประการ (โปรเจ็กต์ที่ดำเนินการตามคำขอติดตามนี้)
|
เนื้อหาการตอบกลับ
การตอบสนองของนาฬิกาต่อข้อความ Push
หากทำสำเร็จ เนื้อหาการตอบกลับจะมีข้อมูลซึ่งมีโครงสร้างดังต่อไปนี้
การแสดง JSON |
{
"historyId": string,
"expiration": string
} |
ช่อง |
historyId |
string
รหัสของระเบียนประวัติปัจจุบันของกล่องจดหมาย
|
expiration |
string (int64 format)
เมื่อ Gmail จะหยุดส่งการแจ้งเตือนสำหรับการอัปเดตกล่องจดหมาย (epoch millis) โปรดโทรหา 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
ลักษณะการกรองของรายการ labelIds ที่ระบุ
Enum |
include |
รับเฉพาะข้อความ Push สำหรับการเปลี่ยนแปลงข้อความที่เกี่ยวข้องกับ labelId ที่ระบุ |
exclude |
รับข้อความ Push สำหรับการเปลี่ยนแปลงข้อความทั้งหมด ยกเว้นการเปลี่ยนแปลงที่เกี่ยวข้องกับ labelId ที่ระบุ |
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 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. |"]]