簡體   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