【问题标题】:Adding Authorization header to GET request将授权标头添加到 GET 请求
【发布时间】:2019-07-20 09:19:31
【问题描述】:

我正在使用新的 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();   
}));
}

【问题讨论】:

    标签: javascript node.js rest alexa-skills-kit airtable


    【解决方案1】:

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

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

    https.request accepts the same options fields as http.reqest. http.request options object allows headers to be defined.

    【讨论】:

    • 好的,但我的响应是作为对象返回的。不确定如何将其字符串化为 json 或我应该做什么?
    猜你喜欢
    • 2023-03-23
    • 2012-05-02
    • 2018-05-04
    • 2021-02-19
    • 2016-09-25
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多