0

我使用下一段代码请求 2fa(双因素身份验证)的代码并且运行良好:

private async void TwilioSMS(object sender, EventArgs e)
{
RestSharp.RestClient client = new RestSharp.RestClient("https://api.authy.com/protected/json/phones/verification");

        RestSharp.RestRequest request = new RestSharp.RestRequest("start", RestSharp.Method.POST);
        numcheck = Numero.Text.Trim();
        request.AddHeader("Content-Type", "application/json");
        request.RequestFormat = RestSharp.DataFormat.Json;
        string este = "{\"api_key\":\"" + apikey + "\",\"via\":\"sms\"," + "\"country_code\":52," + "\"phone_number\":"+numcheck + ",\"locale\":\"es\"}";

    request.AddParameter("text/json", este, RestSharp.ParameterType.RequestBody);
        RestSharp.IRestResponse response = await client.ExecuteTaskAsync(request);

        respuesta = response.Content;
        Console.WriteLine(respuesta);
}

但是当我尝试使用下一段代码进行验证时:

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.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"); }


    }

我得到下一个错误:

{"error_code":"60001","message":"Invalid API key","errors":{"message":"Invalid API key"},"success":false}

我对两者都使用相同的 api 密钥,为什么会发生错误?

4

1 回答 1

1

Twilio 开发人员布道者在这里。

当调用api.authy.com/protected/json/phones/verification/check请求时应该是一个GET请求。您已发出GET请求,但是您在请求正文中将参数作为 JSON 传递。发出GET请求时,您不会传递正文,因此 Authy API 不会读取它们。这些参数应作为查询字符串参数包含在 URL 中,并且您的 API 密钥应在X-Authy-API-Key标头中设置。有关更多详细信息,请参见此处的 curl 示例

于 2018-05-25T05:31:15.337 回答