1

我正在使用新的 SDK 2.0 构建 alexa 技能,但现在无法实现简单的 http get 请求。如何将授权标头添加到 getRemoteData url 请求?下面的代码不起作用。

我正在尝试调用 Airtable API 来获取一些远程数据

const UserReplyIntent_Handler =  {
canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest' && request.intent.name === 'UserReplyIntent' ;
},
async handle(handlerInput) {
    const response = await httpGet();

    console.log(response);

    return handlerInput.responseBuilder
            .speak("Okay. Here is what I got back." + response.records.fields.Indication)
            .reprompt("Would you like to learn more?")
            .getResponse();
},
};     
function httpGet() {
return new Promise(((resolve, reject) => {
    const headers = {
        Authorization: 'Bearer key12345678'
    };
    var options = {
        host: 'api.airtable.com',
        port: 443,
        path: '/v0/appYqfJ3Rt2F0sRGn/Database?filterByFormula=(DrugName=%27azatadine%27)',
        method: 'GET',
    };
    const request = https.request(options, {headers}, (response) => {
        response.setEncoding('utf8');
        let returnData = '';

        response.on('data', (chunk) => {
          returnData += chunk;
        });

        response.on('end', () => {
          resolve(JSON.parse(returnData));
        });

        response.on('error', (error) => {
          reject(error);
        });
      });
      request.end();   
}));
}
4

1 回答 1

3

标头进入选项对象,而不是作为单独的参数:

    var options = {
        host: 'api.airtable.com',
        port: 443,
        path: '/v0/appYqfJ3Rt2F0sRGn/Database?filterByFormula=(DrugName=%27azatadine%27)',
        method: 'GET',
        headers: {
          Authorization: 'Bearer key12345678'
        }
    };

https.request 接受与 http.reqest 相同的选项字段。 http.request 选项对象允许定义标头。

于 2019-07-20T10:31:25.630 回答