處理動作要求
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如「宣告動作」一文所述,當使用者與應用程式內動作互動時,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
的服務檢查:
所有動作要求的 User-Agent 都是 Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)
。
如果所有檢查都通過,服務即可繼續下一個步驟。
步驟 2:處理動作
服務應根據網址參數和向使用者收集的其他資訊處理動作。
使用者提供的額外資訊位於要求主體中,並使用 x-www-form-urlecoded 編碼。資訊會設定在名稱與動作屬性相符的屬性中。舉例來說,ConfirmAction 具有 confirmed
屬性。
步驟 3:傳回回應代碼
服務成功處理並記錄動作後,應傳回回應代碼 200 (OK)
。發生錯誤時,可以使用下列回應碼:
回應代碼 |
實驗組 |
400 (錯誤的要求) |
Google 會導致動作失敗。 |
401 (未獲授權) |
Google 會導致動作失敗。 |
404 (找不到) |
Google 會導致動作失敗。 |
408 (要求逾時) |
Google 稍後會再試一次。 |
如果永久失敗,Google 會告知使用者動作失敗,並請使用者按照電子郵件中的替代說明操作。
延伸閱讀
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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)."]]