简体   繁体   中英

Twilio authentication on AWS Lambda with Node.js

I want to authenticate with Twilio's api using the Node.js https module. My code is essentially:

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) => { ...

The error I receive is TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object . If I remove the auth argument in the options:

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

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

the error I receive is Authentication Error - No credentials provided . This leads me to believe I am not passing the authentication correctly in the options.

( Using request-promise , this method of passing the authentication works; I was trying to see if I could get it to work using a Node.js built-in module )

Twilio developer evangelist here.

The auth property in the options object requires that its value is a string (see the options you can use here ).

So, to correct your options object, you need to concatenate the Account SID and Auth Token with a colon, like this:

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

Let me know if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM