运行 使用 WSGI 的 Flask-Ask 和 Apache2 时如何验证 Alexa 请求

How do I verify an Alexa request when running Flask-Ask and Apache2 with WSGI

我已经使用 Python 构建了一个应用程序,我已经成功地与 Alexa 集成。由于此应用程序非常适合我的家庭,因此我不太关心部署过程,因为您可以 运行 永远处于开发模式(或者看起来)。

然而,在阅读部署过程时,特别是验证请求实际上来自 Amazon 而不是其他人的安全要求时,我了解到这些相同的要求是 无论如何,这是个好主意!

因此,为了部署,亚马逊要求您 verify requests from Amazon 到您的 Alexa 应用程序。基本上,它分为两部分:

我的整个应用程序都是在 Python 中构建的,但 Alexa 前端是在 Flask-Ask 中构建的,并利用 Apache2 和 WSGI。有很多资源可用于学习如何使用 Java、JS 甚至一些直接的 Python 来验证亚马逊请求,但我找不到任何关于如何使用 Flask-Ask 完成此操作的信息.

所以我的问题是如何使用 Flask-ask 完成此操作?

谢谢!

根据 source code(以及文档,由于网站已关闭,我现在无法访问)有这些相关的配置选项:

The Ask instance is given the following configuration variables by calling on Flask's configuration:

ASK_APPLICATION_ID: Turn on application ID verification by setting this variable to an application ID or a list of allowed application IDs. By default, application ID verification is disabled and a warning is logged. This variable should be set in production to ensure requests are being sent by the applications you specify. Default: None

ASK_VERIFY_REQUESTS: Enables or disables Alexa request verification, which ensures requests sent to your skill are from Amazon's Alexa service. This setting should not be disabled in production. It is useful for mocking JSON requests in automated tests. Default: True

因此,默认情况下每个请求都已经过验证。

此外,您可以添加 ASK_APPLICATION_ID 以确保请求来自您的技能:

app = Flask(__name__)
ask = Ask(app, '/')
app.config["ASK_APPLICATION_ID"] = ["skill-id-1", "skill-id-2"] # List of allowed IDs