2017-04-14 190 views
0


我正在實施亞馬遜Alexa的技能。我想確定用戶的地理位置,但http.get請求不起作用。我在郵遞員那裏嘗試了這個請求,並得到了期望的結果,但是無法弄清楚可能是什麼問題。請求網址應該是這樣的https://api.amazonalexa.com/v1/devices/{deviceId}/settings/address,授權標題爲Authorization: Bearer {token}
這裏是我的代碼
Alexa node.js具有授權的http請求

try{ 
    var body = ""; 
    console.log('Authorization ', consentToken); 
    var response = ''; 

    var options = { 
     host: 'api.eu.amazonalexa.com', 
     path: '/v1/devices/'+deviceId+'/settings/address', 
     port: '443', 
     headers: {'Authorization': "Bearer "+consentToken}, 
     method: 'GET' 
    };  

    https.get(options, function(res) { 
     console.log('status', res.statusCode); 
     if (res.statusCode >= 200 && res.statusCode < 400) { 
      res.on('data', function(data_) { response += data_.toString(); }); 
      res.on('end', function() { 
      var data = JSON.parse(response); 
      console.log('data', data); 
      if (data.length > 0) 
       userLocation = data; 
      }); 
     } 
     }).on('error', function(e) { 
       console.log("errrrror",e); 
     }).on('uncaughtException', function (err) { 
       console.log('uncaughtException',err); 
     }); 
    } 
    catch(error) { 
     console.log('getAndProcessUserLocation',error); 
} 

我不知道爲什麼不執行https.get。沒有錯誤發生,但我無法從中獲取任何日誌。
我跟着從THR官方網站 https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/device-address-api

+0

在你提到'https://api.amazonalexa.com/v1/device ...'的問題中,在你的代碼中我看到'api.eu.amazonalexa.com ...'這是正確的嗎? – imTachu

回答

1

的文檔沒有看到你的代碼的其餘部分,我的猜測是,你是不是表明對Alexa的請求要異步處理,所以會話請求之前結束完成。

+0

處理異步請求的最佳做法是什麼? – Coder

+0

這將取決於您使用的是哪個Alexa SDK或框架。我正在使用alexa-app,並且在該框架中,我只需要從意向處理程序返回一個Promise。 –