是否有人有针对LWA和AWS Lambda的端到端解决方案?当我认为我理解了请求/响应协议时,当我试图让它工作时,我会变得更加困惑。
发布于 2018-06-12 02:50:06
网址是:https://api.amazon.com/user/profile。您必须通过以下三种方式之一来传递XML2.0令牌:作为OAuth中的参数、作为标头上的“承载”令牌或作为XML.这里有一个链接:https://login.amazon.com/website。令牌非常长,每个亚马逊都应该进行编码。这对于URL来说可能太长了。我使用了头方法。
public static WebClient client = new WebClient();
public static JObject GetAmznProfile(string token)
{
string amznProfileHost = "https://api.amazon.com/user/profile";
try
{
client.Headers.Remove("user-agent");
client.Headers.Remove("Authorization");
client.Headers.Add("Authorization: bearer " + token);
}
catch { Logger(Constants.SEVERE, "Exception caught trying to modify http headers in \"GetAmznProfile\""); }
finally { }
string body = GetUrl(amznProfileHost);
if (body != null)
{
JObject json = JObject.Parse(body);
return json;
}
else
{
string error = @"{'user_id':'error','name':'error','email':'error'}";
JObject json = JObject.Parse(error);
return json;
}
}
public static string GetUrl(string url)
{
Logger(Constants.DEBUG, "In 'GetUrl' the URL is " + url);
try
{
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string body = reader.ReadToEnd();
data.Close();
reader.Close();
return body;
}
catch (WebException e) { Logger(Constants.INFO, e.ToString() + " exception caught"); return null; }
}发布于 2018-07-18 07:58:34
在我寻找这个问题的答案时,我偶然发现了这个问题。NHLIVES的回答对我来说不是小菜一碟,因为我使用的是node.js。以下代码是不完整的,并从Amazon的示例中进行了修改,但应该是没有错误的。它当然可以改进。但它对我很有效,我希望它对下一个人有用。
还有一件事,模块'request‘在Lambda中不可用,所以你必须提供它。只需在顶层创建一个压缩文件,一个包含‘index.js’和'safe-buffer‘目录的node_modules目录(从本地节点安装中复制),然后将压缩文件上传到Amazon Lambda (在AWS Lambda中:'Code entry type':'Upload a .zip file')。
var AWS = require('aws-sdk');
var requestlib = require('request');
exports.handler = function (request, context) {
var whoami = async function (request) {
var amznProfileURL = 'https://api.amazon.com/user/profile?access_token=';
if (request.directive.header.namespace === 'Alexa.Discovery' && request.directive.header.name === 'Discover') {
amznProfileURL += request.directive.payload.scope.token;
} else {
amznProfileURL += request.directive.endpoint.scope.token;
}
await new Promise(function(resolve, reject) {
log("LWArequest: ", "amznProfileURL ", amznProfileURL);
requestlib(amznProfileURL, function(error, response, body) {
if (response.statusCode == 200) {
var profile = JSON.parse(body);
log("LWArequest: ", "user_id ", body);
resolve();
} else {
log("LWArequest: ", "error ", "can't connect to Amazon Profile Service");
reject();
}
});
}
);
}
whoami(request);
if (request.directive.header.namespace === 'Alexa.Discovery' && request.directive.header.name === 'Discover') {
log("DEBUG:", "Discover request", JSON.stringify(request));
handleDiscovery(request, context, "");
}
function log(message, message1, message2) {
console.log(message + message1 + message2);
}
}您应该会在CloudWatch日志文件中看到类似以下内容:
2018-07-17T23:06:28.108Z 08d795d3-8a16-11e8-b912-0f46b6510aa8
LWArequest: amznProfileURL https://api.amazon.com/user/profile?access_token=Atza|IwEBIP5ozi...
2018-07-17T23:06:28.409Z 08d795d3-8a16-11e8-b912-0f46b6510aa8
LWArequest: user_id
{
"user_id": "amzn1.account.A..........................A"
}希望能有所帮助。
https://stackoverflow.com/questions/50195050
复制相似问题