尝试发布到 Twilio API 时出现 CORS 策略错误

德鲁·林奇

从 VueJS 应用程序中,我试图对 Twilio API 执行简单的 POST 以发送 SMS。执行 POST 时,我收到以下错误:

"Access to XMLHttpRequest at 'https://api.twilio.com/2010-04-01/Accounts/AC42exxxxxxxxxxcfa9c48/SMS/Messages' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field username is not allowed by Access-Control-Allow-Headers in preflight response."

违规代码如下:

sendTwilio(){
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const sFromNumber = process.env.TWILIO_NUMBER;
    const sBaseURL = 'https://api.twilio.com';
    const phoneNumber = parsePhoneNumberFromString(this.sms.to_number,'US') 

    const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${authToken}`,
    'username': `${accountSid}`
    },

    sBodyText='Test'

    this.SmsUrl = sBaseURL + '/2010-04-01/Accounts/' + accountSid + '/SMS/Messages';

    if (phoneNumber.isValid()){
        this.sms.formattedPhone = phoneNumber.number;

        this.postData = 'From=' + sFromNumber 
        + '+To=' + phoneNumber.number 
        + '+Body=' + sBodyText

        axios.post(`${this.SmsUrl}`, this.postData, {
            headers: headers
        })
        .then((response) => {
            console.log(response)   
        })
        .catch((error) => {
            console.log(error)   
        })                    
    }
},

标题中用于用户名的格式有问题还是我的 CORS 设置有问题?

我的 CORS 设置如下:

CORS_ORIGIN_ALLOW_ALL = True

CORS_ORIGIN_WHITELIST = [        
    'http://localhost:8000',
    'http://localhost:8080',
    'http://127.0.0.1:8000'
]
伊夫森在线

Twilio 使用Basic Auth进行身份验证,因此在您POST使用时,axios您需要执行以下操作:

const headers = {
    'Content-Type': 'application/json'
};
const auth = {
    username: accountSid,
    password: authToken
}
[...]
axios.post(this.SmsUrl, this.postData, {
    auth: auth,
    headers: headers
})

我不确定你是如何使用它的。看看问题的评论。您永远不应该在浏览器应用程序中向客户端公开您的 Twilio 凭据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试从 API 获取信息时出现 CORS 策略错误

尝试使用 node.js 连接到 API 时出现 Cors 策略错误

尝试将数据从角度项目发布到Spring Web服务项目时出现CORS错误

尝试访问 Flask API 时出现 CORS 错误

(400)尝试将简单值发布到API时出现错误请求

从JS发布时Laravel API CORS错误

如何设置WordPress和WooCommerce API,以便在发布时不会出现CORS错误?

尝试执行localhost API时在ReactJS应用中出现CORS错误

尝试通过AWS API Gateway代理集成在POST上启用CORS时出现500错误

尝试从Google Drive v2 API打开文件时出现CORS错误

反应:API 302 重定向到同一域时出现 CORS 错误

Microsoft Graph API:尝试在租户上检索策略时出现403禁止错误

React - 从 API 获取,CORS 策略错误

使用Nuxtjs访问Django API时出现CORS错误

从Jquery调用AWS API时出现CORS错误

使用 Spotify Open API 时出现 CORS 错误

在API调用中使用变量时出现CORS错误

选择api门方式的CORS时出现错误

使用 Reddit JSON API 时出现 CORS 错误

javascript signalR客户端:尝试连接到api的CORS策略错误

Angular App运行API的本地实例时遇到“受CORS策略阻止”错误

在 Three.js 中为 TextGeometry 加载字体时出现 CORS 策略错误

尝试发布工件时出现错误

进行 api 调用时,CORS 策略阻止了获取错误

在 javascript 中使用 fetch 请求 API 有 CORS 策略错误

尝试将新问题发布到GitHub存储库时出现错误“ 400错误的请求”

为什么在尝试调用Firebase Cloud Function API时出现此CORS错误,但是如果我处于调试模式,它似乎可以工作?

尝试将db中的数据发布到网页时出现mysql错误

尝试发布到AWS终端节点时出现卷曲错误