【问题标题】:PreSignUP failed with error connect ECONNREFUSED 127.0.0.1:443 in AWS Lambda functionPreSignUP 失败,并在 AWS Lambda 函数中连接 ECONNREFUSED 127.0.0.1:443 错误
【发布时间】:2021-04-21 14:50:16
【问题描述】:

我正在尝试通过 AWS Cognito OAuth 2.0 客户端凭证流来获取访问令牌, 我的代码如下,我尝试在邮递员上运行它并且它有效,我生成了代码以查看请求的结构,使用节点 HTTPS 复制相同的内容,以便我可以在 lambda 函数中编写它,但是它不起作用,因为我不断收到连接被拒绝错误

"use strict";
const https = require("https");
const accessToken = null;

//the client credentials and client secret
const client_id = "xxxxxxx";
const client_secret = "xxxxxxxx";

const secretAndID = `${client_id}:${client_secret}`;
let bufferObj = Buffer.from(secretAndID, "utf-8");
let base64string = bufferObj.toString("base64");
console.log("base 64 string", base64string);

var accessOptions = {
  method: "POST",
  url: "https://xxxxxxx-dev.auth.eu-west-2.amazoncognito.com/oauth2/token",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    Authorization: `Basic ${base64string}`,
  },
};

//the accessToken generator runner
const accessTokenPayload = {
  grant_type: "client_credentials",
  scope: "",
  client_id: 'xxxxxx'
};

exports.handler = (event, context, callback) => {
const aToken = https.request(accessOptions, (res) => {
  let body = "";
  console.log("access token generator status:", res.statusCode);
  console.log("access token generator Response:", res);
  console.log("Headers:", JSON.stringify(res.headers));
  res.setEncoding("utf8");
  res.on("data", (chunk) => (body += chunk));
  res.on("end", () => {
    console.log("Successfully processed HTTPS response");
    body = JSON.parse(body);
    console.log("The access token generator body", body);
    callback(null, event);
  });
});

aToken.on("error", callback);
aToken.write(JSON.stringify(accessTokenPayload));
aToken.end();
}

我总是收到此错误message: "PreSignUp failed with error connect ECONNREFUSED 127.0.0.1:443."

【问题讨论】:

  • 看来您的预注册 Cognito 触发器设置不正确,因为它正在尝试连接到 localhost。您的触发器是如何配置的?
  • @stdunbar 预注册触发器称为 preAuthLambda ,它基本上是在预注册触发器中的 lambda 函数下选择的。起初,特定的 lambda 函数用于将用户电子邮件和姓名存储到我们的数据库中,我手动生成访问令牌并在 process.env.accessToken 中使用它。

标签: node.js amazon-web-services aws-lambda amazon-cognito


【解决方案1】:

所以我已经能够解决我的问题了,显然,使用HTTPS需要你在它可以工作之前分解这种形式的端口,主机名和路径的URL

所以我在这里将其转换为

var accessOptions = {
  method: "POST",
  url: "https://xxxxxxx-dev.auth.eu-west-2.amazoncognito.com/oauth2/token",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    Authorization: `Basic ${base64string}`,
  },
};

我把上面的代码转换成

var accessOptions = {
  method: "POST",
  port: 443,
  hostname: "xxxasdax.amazoncognito.com",
  path: '/oauth2/token',
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": `Basic ${process.env.base64String}`,
  },
};

现在这解决了连接被拒绝的问题。

我必须做的另一件事是对我发送的数据进行编码URI,而不是发送JSON.stringify(payload),我必须执行以下操作

const bodyData = `${encodeURI('grant_type')}=${encodeURI(data.grant_type)}&${encodeURI('scope')}=${encodeURI(data.scope)}`;

这是我将我的有效负载转换为查询字符串,因为接受的内容类型是 "Content-Type": "application/x-www-form-urlencoded",

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2017-05-16
    • 1970-01-01
    • 2017-07-06
    • 2020-08-27
    • 2022-12-23
    • 2019-01-03
    • 2021-05-20
    • 2021-02-06
    相关资源
    最近更新 更多