gpt4 book ai didi

node.js - Promise 在 https 调用中未解析

转载 作者:太空宇宙 更新时间:2023-11-04 00:23:51 25 4
gpt4 key购买 nike

我正在 API Gateway 后面编写一个 AWS Lambda 函数,该函数使用 Facebook 和我们的应用程序 ID 验证 token ,以便我们可以确认用户的身份,以便他可以删除他的资源。用户从 Facebook 登录发送他的用户 ID 和 token ,我检查 token 是否有效以及是否适用于同一用户。在实现中,我使用了 Promise,但问题是第一个 then 根本没有执行。我首先调用 getFacebookAccessToken,以便可以调用 API,并在响应中解析或拒绝,但代码没有达到这一点。这是函数:

'use strict';

const https = require('https');
const querystring = require('querystring');

exports.handler = (event, context, callback) => {

if (typeof event.body.token === 'undefined' || event.body.token === '') {

context.succeed({
"result":"false",
"message": `InputError: token is not defined or it is empty`
});
return;
}
if (typeof event.body.userId === 'undefined' || event.body.userId === '') {

context.succeed({
"result":"false",
"message": `InputError: userId is not defined or it is empty`
});
return;
}

getFacebookAccessToken()
.then((appAccessToken) => {

return checkFacebookId(appAccessToken, event.body.token, event.body.userId);
})
.then((result) => {

console.log('Done');
context.succeed(result);
})
.catch((error) => {

context.succeed(error);
});
};

let checkFacebookId = (appAccessToken, token, facebookId) => {

return new Promise((resolve, reject) => {

let params = {
input_token: token,
access_token: appAccessToken
};

let requestParams = querystring.stringify(params);

let configOptions = {
method: "get",
hostname: "graph.facebook.com",
path: "/debug_token?" + requestParams
};

let request = https.request(configOptions, function (result) {

let response = "";
result.setEncoding('utf8');
result.on('data', function (chunk) {

response += chunk;
});

result.on('end', function () {

const fbResponse = JSON.parse(response);

if (result.statusCode.toString() === '200') {

console.log(`Validating facebook token: OK`);

if (typeof fbResponse.data === 'undefined') {
reject({
"result": "false",
"message": "Token not valid"
});
}
if (fbResponse.data.user_id === facebookId) {
resolve({
"result": "true"
});
} else {
reject({
"result": "false",
"message": "Token not valid"
});
}
}
else {

reject({
"result": "false",
"error": {
"Message": `Failed validating facebook user access token`,
"statusCode": result.statusCode,
"token_valid": fbResponse
}
});
}
});
});

request.on('error', function (error) {
reject({
"result": "false",
"message": "Error in Facebook",
"error": error
});
});

console.log(`Validating facebook token: PENDING`);
request.end();
});
};

let getFacebookAccessToken = () => {

return new Promise((resolve, reject) => {

let params = {
client_id: event.stage.fb_app_id,
client_secret: event.stage.fb_app_secret,
grant_type: "client_credentials"
};

let requestParams = querystring.stringify(params);

let configOptions = {
method: "get",
hostname: "graph.facebook.com",
path: "/oauth/access_token?" + requestParams
};
let request = https.request(configOptions, function (result) {

let responseData = "";
result.setEncoding('utf8');
result.on('data', function (chunk) {

responseData += chunk;
});

result.on('end', function () {
console.log(result);
if (result.statusCode.toString() === '200') {

console.log(`Getting Facebook Access Token: OK`);
resolve(JSON.parse(responseData).access_token);
}
else {

reject({
"result": "false",
"error": {
"Message": `Failed getting facebook access token`,
"statusCode": result.statusCode
}
});
}
});
});

request.on('error', function (error) {
reject({
"result": "false",
"message": "Error in Facebook",
"error": error
});
});

console.log(`Getting Facebook Access Token: PENDING`);
request.end();
});
};

最佳答案

promise 中存在 JavaScript 错误。 event.stage.fb_app_id 和 secret 不在函数的范围内,这是非常明显的,但由于捕获错误是无声的,我得到了一个空对象。

关于node.js - Promise 在 https 调用中未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43593473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com