You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RestSharp调用Authy验证验证码时API Key无效问题排查

问题分析与解决方案

这个问题的核心是GET请求不应该携带请求体(RequestBody),你的验证代码正好犯了这个错误,导致Authy服务器根本没接收到你的API Key,所以返回了"Invalid API key"错误。

为什么发送验证码的代码能正常工作?

你发送验证码的TwilioSMS方法用的是POST请求,POST请求允许在请求体中传递JSON参数,Authy服务器能正确解析到你的API Key,所以一切正常。

验证代码的问题所在

你的VerifCode方法使用了GET请求,但仍然把包含API Key的JSON字符串放在了请求体里。绝大多数HTTP服务会忽略GET请求的请求体,这就意味着Authy服务器完全没收到你的api_key参数,自然会返回无效API Key的错误。

两种修复方案

方案1:将验证请求改为POST方法(推荐,和发送接口保持一致)

修改请求方法为POST,参数传递方式和发送验证码时一致:

private async void VerifCode(object sender, EventArgs e) {
    try {
        if (numcheck == string.Empty) {
            numcheck = Numero.Text.Trim();
        }
        RestSharp.RestClient client = new RestSharp.RestClient("https://api.authy.com/protected/json/phones/verification");
        // 将GET改为POST
        RestSharp.RestRequest request = new RestSharp.RestRequest("check", RestSharp.Method.POST);
        request.AddHeader("Content-Type", "application/json");
        request.RequestFormat = RestSharp.DataFormat.Json;
        string este = "{\"api_key\":\"" + apikey + "\",\"country_code\":52," + "\"phone_number\":" + numcheck + ",\"verification_code\":" + Confirmation.Text.Trim() + "}";
        request.AddParameter("text/json", este, RestSharp.ParameterType.RequestBody);
        RestSharp.IRestResponse response = await client.ExecuteTaskAsync(request);
        respuesta = response.Content;
        Console.WriteLine(numcheck);
        Console.WriteLine(response.Content);
        var listo = respuesta.Split(new string[] { "\"success\":" }, StringSplitOptions.RemoveEmptyEntries)[1];
        if (listo.Contains("true")) {
            await DisplayAlert("Código correcto", "El código es correcto", "OK");
        } else {
            await DisplayAlert("Código incorrecto", "El código es incorrecto o no ha sido mandado", "OK");
        }
    } catch {
        await DisplayAlert("Código incorrecto", "El código es incorrecto o no ha sido mandado", "OK");
    }
}

方案2:保持GET方法,但使用查询参数传递参数

如果一定要用GET,需要把所有参数作为查询参数添加到请求中,而非请求体:

private async void VerifCode(object sender, EventArgs e) {
    try {
        if (numcheck == string.Empty) {
            numcheck = Numero.Text.Trim();
        }
        RestSharp.RestClient client = new RestSharp.RestClient("https://api.authy.com/protected/json/phones/verification");
        RestSharp.RestRequest request = new RestSharp.RestRequest("check", RestSharp.Method.GET);
        // 添加查询参数替代请求体
        request.AddQueryParameter("api_key", apikey);
        request.AddQueryParameter("country_code", "52");
        request.AddQueryParameter("phone_number", numcheck);
        request.AddQueryParameter("verification_code", Confirmation.Text.Trim());
        RestSharp.IRestResponse response = await client.ExecuteTaskAsync(request);
        respuesta = response.Content;
        Console.WriteLine(numcheck);
        Console.WriteLine(response.Content);
        var listo = respuesta.Split(new string[] { "\"success\":" }, StringSplitOptions.RemoveEmptyEntries)[1];
        if (listo.Contains("true")) {
            await DisplayAlert("Código correcto", "El código es correcto", "OK");
        } else {
            await DisplayAlert("Código incorrecto", "El código es incorrecto o no ha sido mandado", "OK");
        }
    } catch {
        await DisplayAlert("Código incorrecto", "El código es incorrecto o no ha sido mandado", "OK");
    }
}

额外建议

为了避免手动拼接JSON字符串出现语法错误,推荐使用匿名对象序列化的方式传递参数,RestSharp会自动帮你处理JSON格式:

// 替换手动拼接的este字符串
var requestBody = new {
    api_key = apikey,
    country_code = 52,
    phone_number = numcheck,
    verification_code = Confirmation.Text.Trim()
};
request.AddJsonBody(requestBody);

内容的提问来源于stack exchange,提问作者Guillermo Gómez Sánchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:12:30