使用Node.js在AWS Lambda上进行Twilio身份验证

时间:2018-09-03 04:30:08

标签: node.js authentication aws-lambda twilio

我想使用Node.js https模块对Twilio的api进行身份验证。我的代码本质上是:

const options = {
    host: 'api.twilio.com',
    path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json',
    auth: {
        user: TWILIO_ACCOUNT,
        pass: TWILIO_API_KEY
    }
};

const req = https.get(options, (res) => { ...

我收到的错误是TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object。如果我在选项中删除了auth参数:

const options = {
    host: 'api.twilio.com',
    path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json'
};

const req = https.get(options, (res) => { ...

我收到的错误是Authentication Error - No credentials provided。这使我相信我没有在选项中正确通过身份验证。

(使用request-promise,这种通过身份验证的方法有效;我试图查看是否可以使用Node.js内置模块使它正常工作)

1 个答案:

答案 0 :(得分:1)

这里是Twilio开发人员的传播者。

auth对象中的options属性要求其值为string(请参见the options you can use here)。

因此,要更正options对象,您需要使用冒号将Account SID和Auth Token连接起来,如下所示:

const options = {
  host: 'api.twilio.com',
  path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json',
  auth: `${TWILIO_ACCOUNT}:${TWILIO_API_KEY}`
}

让我知道是否有帮助。