处理操作请求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如声明操作中所述,当用户与应用内操作互动时,Google 会向操作中声明的网址发送 HTTP 请求。
以下示例向有关费用报告的电子邮件添加了一个 ConfirmAction
按钮:
JSON-LD
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ConfirmAction",
"name": "Approve Expense",
"handler": {
"@type": "HttpActionHandler",
"url": "https://myexpenses.com/approve?expenseId=abc123"
}
},
"description": "Approval request for John's $10.13 expense for office supplies"
}
</script>
微数据
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="potentialAction" itemscope itemtype="http://schema.org/ConfirmAction">
<meta itemprop="name" content="Approve Expense"/>
<div itemprop="handler" itemscope itemtype="http://schema.org/HttpActionHandler">
<link itemprop="url" href="https://myexpenses.com/approve?expenseId=abc123"/>
</div>
</div>
<meta itemprop="description" content="Approval request for John's $10.13 expense for office supplies"/>
</div>
当用户点击该按钮时,Google 会向您的服务发出 HTTP 请求,以记录确认信息。您的服务会收到来自 Google 的以下 HTTP 请求:
POST /approve?expenseId=abc123 HTTP/1.1
Host: your-domain.com
Authorization: Bearer AbCdEf123456
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)
confirmed=Approved
本页的其余部分将介绍 https://your-domain.com/approve?expenseId=abc123
处的服务需要执行哪些操作才能正确处理该操作。其中包括:
第 1 步:验证请求
建议 https://your-domain.com/approve?expenseId=abc123
的服务检查以下内容:
所有操作请求的用户代理均为 Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)
。
如果所有检查均通过,则服务可以继续执行下一步。
第 2 步:处理操作
服务应按照网址参数中的指定处理操作,并处理从用户那里收集的其他信息。
用户的其他信息位于请求的正文中,并使用 x-www-form-urlecoded 编码。信息设置在名称与 Action 的属性对应的属性中。例如,ConfirmAction 具有属性 confirmed
。
第 3 步:返回响应代码
当服务成功处理并记录操作后,应返回响应代码 200 (OK)
。以下响应代码可用于错误情况:
响应代码 |
实验组 |
400 (Bad Request) |
Google 将使该操作失败。 |
401 (Unauthorized) |
Google 将使该操作失败。 |
404(找不到) |
Google 将使该操作失败。 |
408(请求超时) |
Google 会在稍后重试。 |
如果出现永久性故障,Google 会告知用户操作失败,并告知用户应按照电子邮件中的替代说明操作。
延伸阅读
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Handle Action Requests\n\nAs described in [Declaring Actions](/workspace/gmail/markup/actions/declaring-actions), when a user interacts with an In-App Action, Google sends an HTTP request to a URL declared in the action.\n\nThe following example adds a `ConfirmAction` button to an email about an expense report: \n\n### JSON-LD\n\n \u003cscript type=\"application/ld+json\"\u003e\n {\n \"@context\": \"http://schema.org\",\n \"@type\": \"EmailMessage\",\n \"potentialAction\": {\n \"@type\": \"ConfirmAction\",\n \"name\": \"Approve Expense\",\n \"handler\": {\n \"@type\": \"HttpActionHandler\",\n \"url\": \"https://myexpenses.com/approve?expenseId=abc123\"\n }\n },\n \"description\": \"Approval request for John's $10.13 expense for office supplies\"\n }\n \u003c/script\u003e\n\n### Microdata\n\n \u003cdiv itemscope itemtype=\"http://schema.org/EmailMessage\"\u003e\n \u003cdiv itemprop=\"potentialAction\" itemscope itemtype=\"http://schema.org/ConfirmAction\"\u003e\n \u003cmeta itemprop=\"name\" content=\"Approve Expense\"/\u003e\n \u003cdiv itemprop=\"handler\" itemscope itemtype=\"http://schema.org/HttpActionHandler\"\u003e\n \u003clink itemprop=\"url\" href=\"https://myexpenses.com/approve?expenseId=abc123\"/\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003cmeta itemprop=\"description\" content=\"Approval request for John's $10.13 expense for office supplies\"/\u003e\n \u003c/div\u003e\n\nWhen the user clicks on the button, an HTTP request will be issued from Google to your service, recording the confirmation. Your service receives the following HTTP request from Google: \n\n POST /approve?expenseId=abc123 HTTP/1.1\n Host: your-domain.com\n Authorization: Bearer AbCdEf123456\n Content-Type: application/x-www-form-urlencoded\n User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)\n\n confirmed=Approved\n\nThe rest of this page describes what the service at `https://your-domain.com/approve?expenseId=abc123` needs to do to handle the action properly. This includes:\n\n- Verifying the request\n- Processing the payload\n- Returning a response code\n\nStep 1: Verify the Request\n--------------------------\n\nThe service at `https://your-domain.com/approve?expenseId=abc123` is encouraged to check:\n\n- [Limited Use Access Token](/workspace/gmail/markup/actions/limited-use-access-tokens) - To protect against replay attacks.\n- [User Agent](http://en.wikipedia.org/wiki/User_agent) - To make sure the request comes from Google.\n- [Bearer Token](/workspace/gmail/markup/actions/verifying-bearer-tokens) - To guarantee the request coming from Google is intended to the service.\n\nThe User Agent for all Action requests is `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)`.\n\nIf all checks pass, the service can proceed to the next step.\n\nStep 2: Process the Action\n--------------------------\n\nThe service should process the action as specified in the URL parameters as well as additional information collected from the user.\n\nAdditional information from the user resides in the request's body, and is encoded using the [x-www-form-urlecoded](https://en.wikipedia.org/wiki/Percent-encoding#The_application.2Fx-www-form-urlencoded_type) encoding. The information is set in properties whose names correspond with the properties of Action. For example, [ConfirmAction](/workspace/gmail/markup/reference/types/ConfirmAction) has the property `confirmed`.\n\nStep 3: Return a Response Code\n------------------------------\n\nOnce the service processed and recorded the action successfully, it should return a response code `200 (OK)`. The following response codes can be used in error situations:\n\n| Response Code | Treatment |\n|-----------------------|--------------------------------|\n| 400 (Bad Request) | Google will fail the action. |\n| 401 (Unauthorized) | Google will fail the action. |\n| 404 (Not Found) | Google will fail the action. |\n| 408 (Request Timeout) | Google will retry again later. |\n\nIn case of permanent failure, Google will tell the user that action has failed and that the user should follow alternative instructions inside the email.\n\nFurther Reading\n---------------\n\n- [Securing Actions](/workspace/gmail/markup/actions/securing-actions)\n\n| **Note:** Some of the schemas used by Google are still going through the standardization process of [schema.org](http://schema.org), and therefore, may change in the future. [Learn More](/workspace/gmail/markup/reference/schema-org-proposals)."]]