[英]Is there and api to get details of Alexa skill from Amazon Alexa Skill Store
[英]Alexa skill that uses data from an external API with API-KEY
我試圖使用運行在node.js 8.1上的lambda從alexa后端代碼訪問外部API,該代碼可以訪問任何不需要api鍵的終結點,但我找不到找到方法來包含我的authoraztion(api-key )中的代碼,這樣我就可以檢索我正在尋找的數據。 我試圖訪問的api文檔如下:
curl --request GET -H'授權:承載'-H'內容類型:application / json'“ https:// some-end-point / path / i / want ”
這是針對alexa-skills-kit的,它在調用該技能后使用lambda並嘗試訪問帶有api-key的外部api。代碼可以將信息檢索到不需要任何鍵的任何端點。 我已經嘗試過將密鑰作為參數包含在URL中(api密鑰+ URL),因為alexa,lambda,nodejs不熟悉,因為我不確定如何調試它,但我只是沒有得到期望的輸出(這是alexa轉換文本)與從外部api獲取的信息進行語音交流)。
pd:假設我的api密鑰是:xxxx-xxxx-xxxx
// endpoint that i want
url = https://some-end-point/path/i/want
await getRemoteData(url)
.then((response) => {
const data = JSON.parse(response);
outputSpeech = `the data thati want is ${data.records.length} `;
for (let i = 0; i < data.records.length; i++) {
if (i === 0) {
//first record
outputSpeech = outputSpeech + data.records[i].fields.name + ', '
} else if (i === data.records.length - 1) {
//last record
outputSpeech = outputSpeech + 'y '+data.records[i].fields.name +
', '
} else {
//middle record(s)
outputSpeech = outputSpeech + data.records[i].fields.name + ', '
}
}
})
//function getRemoteData
const getRemoteData = function (url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? require('https') : require('http');
const request = client.get(url,(response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed with status code: ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(body.join('')));
});
request.on('error', (err) => reject(err))
})
};
上面的代碼可以訪問任何端點而沒有錯誤,但是我不知道如何包括api密鑰,所以它可以訪問api,預期的輸出是通過包含api-key可以訪問api
這個新手將很高興為您解決這個問題。
您需要傳遞一個options
對象作為client.get
的第二個參數。 例如:
const options = {
headers: {
'Authorization': 'Bearer <your API key>'
}
}
然后在哪里進行請求:
const request = client.get(url, options, (response) => {
// Do the rest of your stuff here...
}
您可以在此處找到有關這些options
更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.