为什么从Azure终结点为Postman响应400,但在Alexa控制台上工作正常?

时间:2018-11-05 19:52:05

标签: azure postman alexa alexa-skills-kit

我对开发Alexa技能并不陌生,因此我将在网上找到的示例用作Azure上托管的C#终结点。它可以在Alexa控制台上正常工作,但是当我尝试使用Postman应用程序测试同一终结点时,出现400错误。

当我使用Alexa控制台时,它将显示发送到端点的JSON输入以及从端点接收的JSON输出。如果我复制JSON输入并将其粘贴到Postman中并将其发送到同一端点,则会收到400错误。显然,我缺少了一些东西。

以下是我的两个源文件和JSON输入。

RollTheDice.cs

public static class RollTheDice
{
    [FunctionName("RollTheDice")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
    log.Info("C# HTTP trigger function processed a request.");

    var speechlet = new RollTheDiceSpeechlet();
    return await speechlet.GetResponseAsync(req);
    }
}

RollTheDiceSpeechlet.cs

public class RollTheDiceSpeechlet : SpeechletBase, ISpeechletWithContext
{
  public SpeechletResponse OnIntent(IntentRequest intentRequest, Session session, Context context)
  {
    try
    {
      // Default to 6 sides if not specified
      if (!int.TryParse(intentRequest.Intent.Slots["DiceType"].Value, out int numSides))
        numSides = 6;

      var rollResults = new Random().Next(Math.Max(1, numSides - 1)) + 1; // Account for random returning '0'
      return new SpeechletResponse
      {
        ShouldEndSession = false,
        OutputSpeech = new PlainTextOutputSpeech { Text = $"I rolled a {numSides} sided die and got a {rollResults}." }
      };
    }
    catch (Exception ex)
    {
      return new SpeechletResponse
      {
        ShouldEndSession = false,
        OutputSpeech = new PlainTextOutputSpeech { Text = ex.Message }
      };
    }

  }

  public SpeechletResponse OnLaunch(LaunchRequest launchRequest, Session session, Context context)
  {
    return new SpeechletResponse
    {
      ShouldEndSession = false,
      OutputSpeech = new PlainTextOutputSpeech { Text = "Welcome to the Roll the Dice. Ask me to roll the dice." }
    };
  }

  public void OnSessionEnded(SessionEndedRequest sessionEndedRequest, Session session, Context context)
  {
    return;
  }

  public void OnSessionStarted(SessionStartedRequest sessionStartedRequest, Session session, Context context)
  {
    return;
  }
}

JSON输入

同样,一切正常,但是当我用Postman测试它时,出现404错误。  端点是我在Visual Studio 201中开发的C#无服务器功能。  在本地运行时,我将URL复制/粘贴到Postman应用程序中并发送帖子。查看随附的屏幕截图。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

由于错误提示,您缺少SignatureSignatureCertChainUrl标头。这些有助于保护您的端点并验证传入请求是否由Alexa发送。来自其他来源的任何请求均应被拒绝。通过测试控制台对其进行测试时,将包含这些标头,您将获得成功的响应。

标题:

Signature 
SignatureCertChainUrl

验证传入的请求分为两部分:

  1. 检查请求签名以验证请求的真实性。
  2. 检查请求时间戳,以确保该请求不是旧请求。

有关验证该请求是否由Alexa here发送的更多信息

相关问题