添加授权 Header
Adding Authorization Header
如何向此示例代码的 header 添加授权密钥?
我想向 getRemoteData url 请求添加授权 header。例如,授权 = Bearer XXXXXXXXXXXX
如何将此添加到我的 URL 请求中?
async handle(handlerInput) {
let outputSpeech = 'This is the default message.';
await getRemoteData('http://api.open-notify.org/astros.json')
.then((response) => {
const data = JSON.parse(response);
outputSpeech = `There are currently ${data.people.length} astronauts in space. `;
for (let i = 0; i < data.people.length; i++) {
if (i === 0) {
//first record
outputSpeech = outputSpeech + 'Their names are: ' + data.people[i].name + ', '
} else if (i === data.people.length - 1) {
//last record
outputSpeech = outputSpeech + 'and ' + data.people[i].name + '.'
} else {
//middle record(s)
outputSpeech = outputSpeech + data.people[i].name + ', '
}
}
})
.catch((err) => {
//set an optional error message here
//outputSpeech = err.message;
});
return handlerInput.responseBuilder
.speak(outputSpeech)
.getResponse();
},
};
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))
})
};
更改此 client.get()
呼叫来自:
const request = client.get(url, (response) => {
至:
const headers = {
Authorization: 'Bearer 1234'
};
const request = client.get(url, {headers}, (response) => {
如何向此示例代码的 header 添加授权密钥?
我想向 getRemoteData url 请求添加授权 header。例如,授权 = Bearer XXXXXXXXXXXX
如何将此添加到我的 URL 请求中?
async handle(handlerInput) {
let outputSpeech = 'This is the default message.';
await getRemoteData('http://api.open-notify.org/astros.json')
.then((response) => {
const data = JSON.parse(response);
outputSpeech = `There are currently ${data.people.length} astronauts in space. `;
for (let i = 0; i < data.people.length; i++) {
if (i === 0) {
//first record
outputSpeech = outputSpeech + 'Their names are: ' + data.people[i].name + ', '
} else if (i === data.people.length - 1) {
//last record
outputSpeech = outputSpeech + 'and ' + data.people[i].name + '.'
} else {
//middle record(s)
outputSpeech = outputSpeech + data.people[i].name + ', '
}
}
})
.catch((err) => {
//set an optional error message here
//outputSpeech = err.message;
});
return handlerInput.responseBuilder
.speak(outputSpeech)
.getResponse();
},
};
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))
})
};
更改此 client.get()
呼叫来自:
const request = client.get(url, (response) => {
至:
const headers = {
Authorization: 'Bearer 1234'
};
const request = client.get(url, {headers}, (response) => {