Alexa 技能套件 -Lambda 函数 - 无法验证 SpeechletRequest (java)

Alexa Skill kit -Lambda function - Could not validate SpeechletRequest (java)

我尝试基于 https://github.com/amzn/alexa-skills-kit-java 创建一个 HelloWorld 技能,但是当我测试 lambda 函数时它显示了这个错误

{
  "errorMessage":"com.amazon.speech.speechlet.SpeechletRequestHandlerException: Could not validate SpeechletRequest null using verifier ApplicationIdSpeechletRequestVerifier, rejecting request",

  "errorType": "java.lang.RuntimeException",

  "stackTrace": [    "com.amazon.speech.speechlet.lambda.SpeechletRequestStreamHandler.handleRequest(SpeechletRequestStreamHandler.java:101)",
 "helloworld.HelloWorldSpeechletRequestStreamHandler.handleRequest(HelloWorldSpeechletRequestStreamHandler.java:43)"

 ],

  "cause": {

    "errorMessage": "Could not validate SpeechletRequest null using 
verifier ApplicationIdSpeechletRequestVerifier, rejecting request",
    "errorType": "com.amazon.speech.speechlet.SpeechletRequestHandlerException",

    "stackTrace": [
      "com.amazon.speech.speechlet.SpeechletRequestHandler.handleSpeechletCall(SpeechletRequestHandler.java:73)",
      "com.amazon.speech.speechlet.lambda.SpeechletRequestStreamHandler.handleRequest(SpeechletRequestStreamHandler.java:98)",
      "helloworld.HelloWorldSpeechletRequestStreamHandler.handleRequest(HelloWorldSpeechletRequestStreamHandler.java:43)"
    ]
  }
}

这是我的 Java 文件

public final class HelloWorldSpeechletRequestStreamHandler extends SpeechletRequestStreamHandler {
    private static final Set<String> supportedApplicationIds = new HashSet<String>();
    static {
        /*
         * This Id can be found on https://developer.amazon.com/edw/home.html#/ "Edit" the relevant
         * Alexa Skill and put the relevant Application Ids in this Set.
         */
        supportedApplicationIds.add("amzn1.echo-sdk-ams.app.[amzn1.echo-sdk-ams.app.56bcdaf9-97fc-47f9-9918-43cb6a90d9f5]");
    }


    public HelloWorldSpeechletRequestStreamHandler() {
        super(new HelloWorldSpeechlet(), supportedApplicationIds);
    }
}

我错过了什么??

我会将 static 代码放到您创建的扩展 SpeechletLambda 的 class 上。我相信,这是在加载此 class 并执行其静态代码之前进行评估并得到解决的地方。

或者,您也可以关闭验证。如果有人非常了解您的开发环境,可以调用您的私有 lambda 函数,那么他们很可能知道足以欺骗您的应用程序 ID。因此,验证它没有太多安全价值。有关关闭它的示例,请参阅 here.

您在支持的应用程序 ID 中的 ID 有误。该 ID 需要是 Alexa Skills 应用程序的 ID,可以在“技能信息”页面上找到它。它应该看起来像这样:

supportedApplicationIds.add("amzn1.ask.skill.c236d019-7d2a-5c96-a02f-ef8ab6f8e023");

我知道演示有 [place id here] 但你真的替换了整个东西。

对我来说,我得到了这个异常,因为我试图 运行 我的 lambda 函数没有适当的测试事件 JSON 在操作选项卡下。如果您单击 'Actions' 选项卡,然后单击 'Configure Test Event',您应该以它可以解释的 JSON 形式提供函数输入。仔细查看后,我发现您可以通过转到开发人员控制台来获得此 JSON,您在开发人员控制台中创建了具有所有技能配置的技能。在左侧单击 'Test' 选项卡,然后转到显示 'Service Simulator' 的部分。有一个显示 'Enter Utterance' 的文本框,您可以在其中以文本形式为您的功能输入语音命令,例如 'Alexa tell [yourApp] to say Hello'。单击 'Ask [yourApp] ' 按钮,将在左侧框中生成 Lambda 请求 JSON,并在右侧显示输出。然后只需将左侧的 JSON 复制并粘贴到 lambda 控制台中的测试事件中,然后就可以了。

我试图创建包含在 https://github.com/amzn/alexa-skills-kit-java 中的地址技能,但我遇到了相同类型的错误。

事实证明,问题出在 DeviceAddressSpeechletRequestStreamHandler 和在静态 {} 块中创建 Set<String> supportedApplicationIds 的实例。

当我将 new HashSet<>(); 移动到声明 class 的属性时,它开始工作了。