用restsharp验证代码authy

时间:2018-05-17 00:45:44

标签: c# twilio authy

我使用下一段代码来索取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密钥,为什么会发生错误?

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

拨打api.authy.com/protected/json/phones/verification/check时,请求应为GET个请求。您已发出GET请求,但是您在请求正文中将参数作为JSON传递。在发出GET请求时,您不会传递正文,因此Authy API不会读取它们。参数应作为查询字符串参数包含在URL中,并且您的API密钥应在X-Authy-API-Key标头中设置。请参阅the curl example here for more detail