【问题标题】:AWS Amplify MissingRequiredParameter userId errorAWS Amplify MissingRequiredParameter userId 错误
【发布时间】:2019-01-11 14:31:22
【问题描述】:

我遵循从Interactions 开始的指南。当我在 Interactions 上调用 send 方法时,出现以下错误:

(节点:27796)UnhandledPromiseRejectionWarning:MissingRequiredParameter:参数中缺少必需的键“userId”

Interactions 似乎需要一个 userId 参数,该参数在 @aws-amplify/interactions/lib/Providers/AWSLexProvider.js 中应该是从 credentials.identityId 中提取的。但是,当我登录credentials 时,它是类型SharedIniFileCredentials,它没有identityId 属性according to the documentation

来自reading the docsidentityId 必须是 Cognito 用户。 AWSLexProvider.js 不会尝试调用 CognitoIdentityCredentials 来获取 Cognito 凭据。

因此,我不确定identityId 应该来自哪里

我的代码是来自 Amplify 网站的示例:

import Amplify, { Interactions } from 'aws-amplify';
import aws_exports from './aws-exports';

Amplify.configure(aws_exports);

async function test() {
    let userInput = "I want to reserve a hotel for tonight";

    // Provide a bot name and user input
    const response = await Interactions.send("BookTrip", userInput);

    // Log chatbot response
    console.log (response['message']);
}

test();

那么我在这里错过了什么?

【问题讨论】:

  • 有同样的问题。如果您知道如何解决它,请告诉我?

标签: amazon-web-services aws-sdk amazon-lex aws-amplify amplifyjs


【解决方案1】:

我在添加一个没有使用完整放大设置手动创建的机器人时遇到了同样的问题,但只是使用放大 React 前端 SDK 来使用聊天机器人组件。原来我为 Cognito Auth 使用了错误的 identityPoolId。当使用正确的,可以在 cognito federated identities 部分中的where to find identity pool id 中找到,错误消失并且机器人开始工作。此外,我保证分配给该身份池的 custom_auth_role 在操作下还具有以下属性:

            "Action": [
             ...
            "lex:PostContent",
            "lex:PostText"
        ],

这可以在该角色的 IAM -> 角色部分中分配。不确定这是否是绝对必要的。

所以最后是这样的:

    //...all other React imports, etc
    import { ChatBot, withAuthenticator } from "aws-amplify-react";
    import Amplify, { Auth } from "aws-amplify";

    Amplify.configure({
         Auth: {
          identityPoolId: "eu-west-1:XX-XX-XX-XXX-XXX", //<-here the right Id needs to be set
          region: "eu-west-1",
          userPoolId: "eu-west-1_XXXXXX",
          userPoolWebClientId: "XXXXXX"
         },
         Interactions: {
          bots: {
           botNameAsInAwsConsole: {
            name: "someName",
            alias: "$LATEST",
            region: "eu-west-1"
           }
         }
        }
    });

    function App() {
     return (
      <ChatBot
        title="Demo Bot"
        theme={myTheme}
        botName="botNameAsInAwsConsole"
        welcomeMessage="Welcome, how can I help you today?"
        onComplete={handleComplete}
        clearOnComplete={true}
        conversationModeOn={false}
        voiceEnabled={false}
      />
  );
}

export default withAuthenticator(App, true);

【讨论】:

    【解决方案2】:

    我之前没有使用过 AWS Amplify,所以这个答案可能不是原因,但我之前已经多次使用过 Amazon Lex。这要查找的 UserId 字段可能是 Lex PostText/PostContent 请求的 UserId 参数(请参见下面的代码)

    来自PostText Documentation:

    var params = {
      botAlias: 'STRING_VALUE', /* required */
      botName: 'STRING_VALUE', /* required */
      inputText: 'STRING_VALUE', /* required */
      userId: 'STRING_VALUE', /* required - THIS IS MISSING FROM YOUR EXAMPLE */
      requestAttributes: {
        '<String>': 'STRING_VALUE',
        /* '<String>': ... */
      },
      sessionAttributes: {
        '<String>': 'STRING_VALUE',
        /* '<String>': ... */
      }
    };
    lexruntime.postText(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
    

    还有PostContent Documentation:

    var params = {
      botAlias: 'STRING_VALUE', /* required */
      botName: 'STRING_VALUE', /* required */
      contentType: 'STRING_VALUE', /* required */
      inputStream: new Buffer('...') || 'STRING_VALUE' || streamObject, /* required */
      userId: 'STRING_VALUE', /* required - THIS IS MISSING FROM YOUR EXAMPLE */
      accept: 'STRING_VALUE',
      requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
      sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
    };
    lexruntime.postContent(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
    

    现在,就像我之前所说的,我以前没有使用过 AWS Amplify,所以老实说我不确定,但希望这能为您指明正确的方向。

    【讨论】:

      【解决方案3】:

      您只需使用附加了“运行 AWS lex 聊天机器人”权限角色组/策略的用户登录。

      【讨论】:

        猜你喜欢
        • 2020-05-10
        • 2022-01-02
        • 2021-08-25
        • 2021-03-17
        • 2022-09-27
        • 2021-07-26
        • 2020-05-08
        • 1970-01-01
        • 2022-08-17
        相关资源
        最近更新 更多