动态的个性化电子邮件内容通常需要对用户进行身份验证。不过,为了保护用户数据,从 Gmail 内的 AMP 电子邮件内发出的所有 HTTP 请求都会被代理,并会从 Cookie 中删除。
如需对来自 AMP 电子邮件的请求进行身份验证,您可以使用访问令牌。
访问令牌
您可以使用访问令牌对用户进行身份验证。访问令牌由电子邮件发件人提供和检查。发件人使用令牌来确保只有有权访问 AMP 电子邮件的用户才能发出该电子邮件中包含的请求。访问令牌必须具有加密安全性,并且有时间和范围限制。相应请求包含在相应请求的网址中。
此示例演示了如何使用 <amp-list>
显示经过身份验证的数据:
<amp-list src="https://example.com/endpoint?token=REPLACE_WITH_YOUR_ACCESS_TOKEN"
height="300">
<template type="amp-mustache">
...
</template>
</amp-list>
同样,在使用 <amp-form>
时,请将您的访问令牌放在 action-xhr
网址中。
<form action-xhr="https://example.com/endpoint?token=REPLACE_WITH_YOUR_ACCESS_TOKEN" method="post">
<input type="text" name="data">
<input type="submit" value="Send">
</form>
示例
以下示例考虑了一个虚构的记事服务,该服务允许已登录的用户向自己的帐号添加记事,以便稍后查看。服务想要向用户 jane@example.com
发送一封电子邮件,其中包含该用户之前记下的记事列表。用户可以在端点 https://example.com/personal-notes
上找到当前用户的备注列表(JSON 格式)。
在发送电子邮件之前,该服务会为 jane@example.com: A3a4roX9x
生成加密安全的有限使用访问令牌。访问令牌包含在网址查询内的字段名称 exampletoken
中:
<amp-list src="https://example.com/personal-notes?exampletoken=A3a4roX9x" height="300">
<template type="amp-mustache">
<p>{{note}}</p>
</template>
</amp-list>
端点 https://example.com/personal-notes
负责验证 exampletoken
参数并查找与令牌关联的用户。
如需了解详情,请参阅限制使用的访问令牌。