我正在学习Alexa技能套件,并通过简单的技巧来实现Alexa设备地址api。但是,当我在AWS平台上测试代码时,它返回“ null”作为响应,并在日志中得到:

{ Error: connect ECONNREFUSED 127.0.0.1:443
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 443 }


(不是日志中的所有信息,但我认为是导致问题的部分)

这是我的代码:

function locationIntent(context,callback) {
var cardTitle = 'Location';
var deviceId = context.context.System.device.deviceId;
var accessToken = context.context.System.apiAccessToken;
var endpoint = context.context.System.apiEndpoint;
var url = endpoint+"/v1/devices/"+deviceId+"/settings/address";
var options = {
    Host: "api.amazonalexa.com",
    Endpoint:"/v1/devices/"+deviceId+"/settings/address",
    Authorization: "Bearer" +accessToken
      };

getLocation(options,function(rep,err){
    if(err){
        console.log(err);
    }else{
         var speechOutput = "Your adress is "+rep.addressLine1;
         var repromptText = speechOutput;
         var shouldEndSession = true;
         callback({},
            buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
            }});
}
function getLocation(options,callback){
https.get(options,function(res){
    var body = '';
    res.on('data',function(chunk){
        body+=chunk;
    });

    res.on('end',function(){
        var result = JSON.parse(body);
        console.log(result);
        try{
        callback(result);
    }catch(e){
        console.log("error\n"+e);
        callback("Something is wrong");
    }
    });
}).on('error',function(e){
    console.log("error in api:"+e);
    callback('',e);
});
}


因此,我真的很想知道代码中的问题。谢谢你们 :)

最佳答案

https.get(options, ...,在选项中,我看到您有Host: "api.amazonalexa.com",,因此您正在尝试连接到该地址,对吗?

但是我看到它实际上是在尝试连接到127.0.0.1吗?

您是否可能在主机文件中进行了任何更改并将api.amazonalexa.com映射到127.0.0.1

还是您的DNS服务器正在这样做?

您可以尝试在命令提示符nslookup api.amazonalexa.com中运行以下命令并查看返回的内容吗?



LE:

请参见此处https://nodejs.org/api/http.html#http_http_request_options_callback Host应该是小写的host。如果将其大写,它将不使用它,并且默认为localhost,即您错误中的127.0.0.1。它还说hostname is preferred over host,所以请使用该名称。 Endpoint甚至不存在。它应该是pathAuthorization应该是auth。从那里阅读文档和示例,它应该可以工作。

10-04 22:13
查看更多