【发布时间】: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