繁体   English   中英

AWS Amplify MissingRequiredParameter userId 错误

[英]AWS Amplify MissingRequiredParameter userId error

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

(节点:27796)未处理的PromiseRejectionWarning:MissingRequiredParameter:参数中缺少必需的键“userId”

看起来 Interactions 需要一个userId参数,该参数在@aws-amplify/interactions/lib/Providers/AWSLexProvider.js中应该是从credentials.identityId提取的。 但是,当我记录credentials ,它是SharedIniFileCredentials类型, 根据文档,它没有identityId属性。

通过阅读文档identityId必须是 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();

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

添加一个我使用完整的放大设置手动创建的机器人时,我遇到了同样的问题,但只是使用放大反应前端 SDK 来使用聊天机器人组件。 原来我在 Cognito Auth 中使用了错误的identityPoolId 当使用正确的方法时,如此处所述,在 cognito 联合身份部分中可以找到身份池 ID的位置,错误消失了,机器人开始工作。 此外,我放心, custom_auth_role分配给身份池有addtionally下行动以下特性:

            "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);

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

来自PostText 文档:

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 文档:

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,所以老实说我不确定,但希望这为您指明了正确的方向。

您只需要使用附加了“运行 AWS lex chatbot”权限角色组/策略的用户登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM