AWS Lambda 中的 HTTP GET 只工作一次
HTTP GET within AWS Lambda only works once
我正在尝试使用 Amazon 用户个人资料 API 查找用户个人资料信息,但我的 GET 请求仅有效一次。第一次,配置文件信息 returned 没问题,但所有后续调用 lambda 函数都会导致 GET return 400 错误请求。这是我当前的代码:
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
// console.log(event);
if (event.session.user.accessToken === undefined) {
alexa.emit(':tellWithLinkAccountCard',
'To start using the app, please use the Alexa companion app to authenticate on Amazon');
} else {
amazonProfileURL += event.session.user.accessToken;
console.log(amazonProfileURL);
http.get(amazonProfileURL, (res) => {
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error(`Request Failed.\n` +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\n` +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
let parsedData = JSON.parse(rawData);
user_name = parsedData.name;
user_email = parsedData.email;
console.log(user_name);
console.log(user_email);
alexa.registerHandlers(handlers);
alexa.execute();
} catch (e) {
console.log(e.message);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
第一次调用该函数时,代码成功运行了 alexa.execute()
函数,但在所有后续调用中,控制台都会打印以下内容:
START RequestId: 89a3a696-1442-11e7-9c15-6de8cce4b94d Version: $LATEST
2017-03-29T05:42:46.772Z 89a3a696-1442-11e7-9c15-6de8cce4b94d https://api.amazon.com/user/profile?access_token=<long user access token>
2017-03-29T05:42:46.888Z 89a3a696-1442-11e7-9c15-6de8cce4b94d Request Failed.
Status Code: 400
我对 JavaScript 和 AWS Lambda 还很陌生,非常感谢您的帮助!
我解决了这个问题。事实证明这是一个 AWS Lambda 问题(功能?)而不是 HTTP GET 问题。我的 amazonProfileURL
字符串在对 lambda 函数的调用之间持续存在,因此它只是第一次调用的有效 URL。解决方案是将该行更改为 requestURL = amazonProfileURL + event.session.user.accessToken
,这样 amazonProfileURL
就不会被覆盖。
我正在尝试使用 Amazon 用户个人资料 API 查找用户个人资料信息,但我的 GET 请求仅有效一次。第一次,配置文件信息 returned 没问题,但所有后续调用 lambda 函数都会导致 GET return 400 错误请求。这是我当前的代码:
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
// console.log(event);
if (event.session.user.accessToken === undefined) {
alexa.emit(':tellWithLinkAccountCard',
'To start using the app, please use the Alexa companion app to authenticate on Amazon');
} else {
amazonProfileURL += event.session.user.accessToken;
console.log(amazonProfileURL);
http.get(amazonProfileURL, (res) => {
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error(`Request Failed.\n` +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\n` +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
let parsedData = JSON.parse(rawData);
user_name = parsedData.name;
user_email = parsedData.email;
console.log(user_name);
console.log(user_email);
alexa.registerHandlers(handlers);
alexa.execute();
} catch (e) {
console.log(e.message);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
第一次调用该函数时,代码成功运行了 alexa.execute()
函数,但在所有后续调用中,控制台都会打印以下内容:
START RequestId: 89a3a696-1442-11e7-9c15-6de8cce4b94d Version: $LATEST
2017-03-29T05:42:46.772Z 89a3a696-1442-11e7-9c15-6de8cce4b94d https://api.amazon.com/user/profile?access_token=<long user access token>
2017-03-29T05:42:46.888Z 89a3a696-1442-11e7-9c15-6de8cce4b94d Request Failed.
Status Code: 400
我对 JavaScript 和 AWS Lambda 还很陌生,非常感谢您的帮助!
我解决了这个问题。事实证明这是一个 AWS Lambda 问题(功能?)而不是 HTTP GET 问题。我的 amazonProfileURL
字符串在对 lambda 函数的调用之间持续存在,因此它只是第一次调用的有效 URL。解决方案是将该行更改为 requestURL = amazonProfileURL + event.session.user.accessToken
,这样 amazonProfileURL
就不会被覆盖。