Twilio 频道 - SMS 60200 无效参数错误(400 错误请求)

问题描述 投票:0回答:5

我在使用 Twilio API 时收到“400 - 错误请求”错误,但找不到原因。

这是我的要求:

curl --location --request POST 'https://verify.twilio.com/v2/Services/xxxxxxxxxxxxxx/Verifications' \
--header 'Authorization: Basic xxxxxxxxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'Channel=sms' \
--data-urlencode 'To=+44xxxxxxxxxx'

我收到回复:

{
    "code": 60200,
    "message": "Invalid parameter",
    "more_info": "https://www.twilio.com/docs/errors/60200",
    "status": 400
}

响应包含

RequestID
标头,但在 Twilio 门户中无法通过
RequestID
查找错误。

twilio-api http-status-code-400 bad-request
5个回答
1
投票

您的示例未遵循下面列出的产品文档示例(有效):

https://www.twilio.com/docs/verify/api

curl -X POST https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications \
--data-urlencode "To=+15017122661" \
--data-urlencode "Channel=sms" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

1
投票

我通过使用

SERVICE_SID
而不是
ACCOUNT_SID
解决了我的问题。这是我的代码的样子

import twilio from "twilio";

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);
...
const { sid } = await client.verify.services.create({
      friendlyName: "My First Verify Service",
    });

const response = await client.verify.services(sid).verifications.create({
      to,
      channel: "call", // sms, call, or email
});

0
投票

即使参数正确,我也遇到了相同的错误消息。我在请求 URL 中使用了帐户 SID,而它应该是已创建的验证服务的服务 SID。

在 url 中使用帐户 SID 解决了我的错误。


0
投票

我也遇到过这个问题,twilio 文档中似乎有些混乱,但是我确实在他们的文档中找到了一个位置,显示了设置此问题的正确方法。

来自官方文档https://www.twilio.com/docs/verify/quickstarts/node-express

解决方案是使用

serviceSid
而不是
accountSid
,因为它可能在文档的某些部分中建议。

在代码中它应该如下所示:

client.verify.v2.services(SERVICE_SID)

请注意,am accountSid 带有 AC 前缀(即 AC3265cbdh...) 并且 serviceSid 以 VA 为前缀(即 VA3265cbdh...)

如上面文档链接中所示示例中的硬编码所示


0
投票

将 Twilio 与 Golang 一起使用我遇到了同样的问题 - 这是我解决的方法 ->

这是我的职责

func (app *Config) twilioSendOTP(phoneNumber string) (string, error) {
params := &twilioApi.CreateVerificationParams{}
params.SetTo(phoneNumber)
params.SetChannel("sms")
params.SetChannel("whatsapp")

resp, err := client.VerifyV2.CreateVerification(envServicesSid(), params)
if err != nil {
    return "", err
}

return *resp.Sid, nil
}

func (app *Config) twilioVerifyOTP(phoneNumber string, code string) error {

params := &twilioApi.CreateVerificationCheckParams{}
params.SetTo(phoneNumber)
params.SetCode(phoneNumber)

resp, err := client.VerifyV2.CreateVerificationCheck(envAccountSid(), params)
if err != nil {
    return err
}
if *resp.Status != "approved"{
    return errors.New("not a valid code")
}
return nil
}

我错误地使用了 envAccountSid() 这个函数有我从 twilliosendOTP 函数中的 .env 获得的 AccountSid,我应该用 envServicesSid() 替换它,以便 Twilio 可以验证我的服务。我的新功能看起来像这样 -

func (app *Config) twilioSendOTP(phoneNumber string) (string, error) { params := &twilioApi.CreateVerificationParams{} params.SetTo(phoneNumber) params.SetChannel("sms") params.SetChannel("whatsapp") resp, err := client.VerifyV2.CreateVerification(envServicesSid(), params) if err != nil { return "", err } return *resp.Sid, nil } func (app *Config) twilioVerifyOTP(phoneNumber string, code string) error { params := &twilioApi.CreateVerificationCheckParams{} params.SetTo(phoneNumber) params.SetCode(phoneNumber) resp, err := client.VerifyV2.CreateVerificationCheck(envServicesSid(), params) if err != nil { return err } if *resp.Status != "approved"{ return errors.New("not a valid code") } return nil }
我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.