NodeJS Axios对Twilio的POST请求返回身份验证错误?

ruarlubt  于 2023-01-16  发布在  Node.js
关注(0)|答案(5)|浏览(126)

在Node.js中,我尝试使用Axios向Twilio发送POST请求,并向我的手机发送SMS消息。但我收到一个“错误:验证错误-没有提供凭据?下面是代码:

const body = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
  Body: 'hi from vsc',
  To: toNumber,
  From: fromNumber,
};

const headers = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
  Authorization: `Basic ${accountSID}:${authToken}`,
};

exports.axios = () => axios.post(`https://api.twilio.com/2010-04-01/Accounts/${accountSID}/Messages.json`, body, headers).then((res) => {
  console.log(res, 'res');
}).catch((err) => {
  console.log(err);
});

我也尝试在POSTMAN上使用相同的参数,POST请求成功。我也尝试将我的授权用户名和密码编码为Base 64,但没有成功。我写信给Twilio客户帮助,但还没有收到任何回复。

6tr1vspr

6tr1vspr1#

Axios提供了auth选项,该选项接受带有usernamepassword选项的对象。您可以将username设置为您的帐户SID,将password设置为您的授权令牌。
headers对象应该作为第三个参数中的配置对象的headers参数发送到axios.post,如下所示:

const params = new URLSearchParams();
params.append('Body','Hello from vcs');
params.append('To',toNumber);
params.append('From',fromNumber);

const headers = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
};

exports.axios = () => axios.post(
  `https://api.twilio.com/2010-04-01/Accounts/${accountSID}/Messages.json`,
  params,
  { 
    headers,
    auth: {
      username: accountSID,
      password: authToken
    }
  }
}).then((res) => {
  console.log(res, 'res');
}).catch((err) => {
  console.log(err);
});
shyt4zoc

shyt4zoc2#

头实际上是配置的一个字段,尝试如下:

const config = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    Authorization: `Basic ${accountSID}:${authToken}`,
  }
}

axios.post(URL, data, config).then(...)
00jrzges

00jrzges3#

或者这个(调用Twilio端点的一般示例)

const axios = require('axios');

const roomSID = 'RM1...';
const participantSID = 'PA8...';

const ACCOUNT_SID = process.env.ACCOUNT_SID;
const AUTH_TOKEN = process.env.AUTH_TOKEN;

 const URL = "https://insights.twilio.com/v1/Video/Rooms/"+roomSID+"/Participants/"+participantSID;
    axios({
      method: 'get',
      url: URL,
      auth: {
        username: ACCOUNT_SID,
        password: AUTH_TOKEN
      }
    })
      .then((response) => {
        console.log(response.data);
      })
      .catch((error) => {
          console.log(error);
      });
dsekswqp

dsekswqp4#

工作代码:

const params = new URLSearchParams();

params.append('Body','Hello from vcs');
params.append('To',toNumber);
params.append('From',fromNumber);

exports.axios = () => axios.post(
  `https://api.twilio.com/2010-04-01/Accounts/${accountSID}/Messages.json`,
  params,
  {
    auth: {
      username: accountSID,
      password: authToken,
    },
  },
).then((res) => {
  console.log(res, 'res');
}).catch((err) => {
  console.log(err);
});
m528fe3b

m528fe3b5#

以前的解决方案对我不起作用。我遇到了Can't find variable: btoa错误或A 'To' phone number is required.
使用qs对我很有效:

import qs from 'qs';
import axios from 'axios';

const TWILIO_ACCOUNT_SID = ""
const TWILIO_AUTH_TOKEN = ""
const FROM = ""
const TO = ""

const sendText = async (message: string) => {
  try {
    const result = await axios.post(
      `https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json`,
      qs.stringify({
        Body: message,
        To: TO,
        From: FROM,
      }),
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        auth: {
          username: TWILIO_ACCOUNT_SID,
          password: TWILIO_AUTH_TOKEN,
        },
      },
    );
    console.log({result});
  } catch (e) {
    console.log({e});
    console.log({e: e.response?.data});
  }
};

相关问题