Error: AWS Web RTC Verification with Cognito

Error: AWS Web RTC Verification with Cognito

我正在尝试将我的 javascript 应用程序 AWS WebRTC 与 Cognito 用户连接以动态获取其凭据,但遇到以下错误代码:

json.js:52 Uncaught (in promise) CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

我已经将我的凭据硬编码到配置中并且它已成功运行,但这是一种不好的做法,因此我正在尝试通过我的 Cognito 用户获取 AWS Web RTC 凭据。我已经在我的联邦身份中设置了用户池和 link。凭据现在由 AWS Amplify 管理,它从配置文件 (./aws/credentials).

加载 AWS 用户配置文件

遵循这两个指南:

  1. https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-identity-pools.html
  2. https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html

我已经编写了以下代码片段来获取我的凭据,但现在遇到了错误。

AWS.config.region = '<REGION>';
  AWS.config.update({
    credentials: new AWS.CognitoIdentityCredentials({
      IdentityPoolId: '<Region>:<IdentitiyPoolID>',
      Logins: {
        'cognito-idp.<Region>.amazonaws.com/<UserPoolID> ': <id token from cognito>,
      },
    }),
  });

  var accessKeyId;
  var secretAccessKey;

  AWS.config.credentials.get(function () {
    accessKeyId = AWS.config.credentials.accessKeyId;
    secretAccessKey = AWS.config.credentials.secretAccessKey;
  });

const state = {
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey,
    region: 'region',
}
async function startPlayerForViewer();

非常感谢任何帮助!在网上找不到很多更新 resources/examples。

credentials.get() 是一个异步操作,它在回调中接收键,而状态定义发生在接收到回调之前。因此,密钥未设置。您需要确保在回调之后定义状态,并确保之后发生连接。