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