From 94a153c3853315085bfbd2fe2cc6887b5d8ff18b Mon Sep 17 00:00:00 2001 From: itsokto Date: Sat, 23 May 2020 02:30:31 +0300 Subject: [PATCH] =?UTF-8?q?AuthorizationFormHtmlParser:=20=D1=83=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D1=88=D0=B8=D1=82=D1=8C=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #625 --- .../AuthorizationFormHtmlParser.cs | 11 +++++ VkNet/Model/VkAuthError.cs | 42 +++++++++++++++++++ VkNet/Utils/Utilities.cs | 21 ++++++++++ 3 files changed, 74 insertions(+) create mode 100644 VkNet/Model/VkAuthError.cs diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/AuthorizationFormHtmlParser.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/AuthorizationFormHtmlParser.cs index 1dbd9d00b..64456a493 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/AuthorizationFormHtmlParser.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/AuthorizationFormHtmlParser.cs @@ -6,6 +6,7 @@ using JetBrains.Annotations; using VkNet.Abstractions.Utils; using VkNet.Exception; +using VkNet.Model; using VkNet.Utils; namespace VkNet.Infrastructure.Authorization.ImplicitFlow @@ -30,6 +31,16 @@ public async Task GetFormAsync(Uri url) { var response = await _restClient.PostAsync(url, Enumerable.Empty>()); + if (!response.IsSuccess) + { + throw new VkAuthorizationException(response.Message); + } + + if (Utilities.TryDeserializeObject(response.Value, out var authError)) + { + throw new VkAuthorizationException(authError.ErrorDescription); + } + var doc = new HtmlDocument(); doc.LoadHtml(response.Value); var formNode = GetFormNode(doc); diff --git a/VkNet/Model/VkAuthError.cs b/VkNet/Model/VkAuthError.cs new file mode 100644 index 000000000..49b244b53 --- /dev/null +++ b/VkNet/Model/VkAuthError.cs @@ -0,0 +1,42 @@ +using System; +using Newtonsoft.Json; + +namespace VkNet.Model +{ + /// + /// Authorization error. + /// + [Serializable] + public class VkAuthError + { + /// + /// Error. + /// + [JsonProperty("error")] + public string Error { get; set; } + + /// + /// Error type. + /// + [JsonProperty("error_type")] + public string ErrorType { get; set; } + + /// + /// Error description. + /// + [JsonProperty("error_description")] + public string ErrorDescription { get; set; } + + /// + /// Captcha id. + /// + [JsonProperty("captcha_sid")] + public ulong? CaptchaSid { get; set; } + + /// + /// Captcha image Uri. + /// + [JsonProperty("captcha_img")] + public Uri CaptchaImg { get; set; } + } +} \ No newline at end of file diff --git a/VkNet/Utils/Utilities.cs b/VkNet/Utils/Utilities.cs index 8155f4463..dda2f2d0d 100644 --- a/VkNet/Utils/Utilities.cs +++ b/VkNet/Utils/Utilities.cs @@ -118,5 +118,26 @@ public static string SerializeToJson(T @object) return result == "null" ? null : result; } + + /// + /// Deserializes JSON for the specified .NET type. A return value indicates whether deserialization succeeded. + /// + /// The JSON to deserialize. + /// Deserialized object. + /// Type for deserialization. + /// + public static bool TryDeserializeObject(string json, out T result) + { + try + { + result = JsonConvert.DeserializeObject(json); + return true; + } + catch + { + result = default; + return false; + } + } } } \ No newline at end of file