Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Восстановил авторизацию через логин/пароль #1247

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions VkNet.Tests/Categories/Users/UsersCategoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -560,7 +560,7 @@ public void Get_NotAccessToInternet_ThrowVkApiException()
{
Mock.Get(Api.RestClient)
.Setup(f =>
f.PostAsync(It.IsAny<Uri>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>(), Encoding.UTF8))
f.PostAsync(It.IsAny<Uri>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>(), Encoding.UTF8, null))
.Throws(new VkApiException("The remote name could not be resolved: 'api.vk.com'"));

FluentActions.Invoking(() => Api.Users.Get(new long[]
Expand Down
4 changes: 2 additions & 2 deletions VkNet.Tests/Infrastructure/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public BaseTest()
Mocker.Setup<IRestClient, Task<HttpResponse<string>>>(x =>
x.PostAsync(It.Is<Uri>(s => s == new Uri(Url)),
It.IsAny<IEnumerable<KeyValuePair<string, string>>>(),
It.IsAny<Encoding>()))
It.IsAny<Encoding>(), null))
.Callback(Callback)
.Returns(() =>
{
Expand All @@ -103,7 +103,7 @@ public BaseTest()

Mocker.Setup<IRestClient, Task<HttpResponse<string>>>(x => x.PostAsync(It.Is<Uri>(s => string.IsNullOrWhiteSpace(Url)),
It.IsAny<IEnumerable<KeyValuePair<string, string>>>(),
It.IsAny<Encoding>()))
It.IsAny<Encoding>(), null))
.Throws<ArgumentException>();

Api = Mocker.CreateInstance<VkApi>();
Expand Down
5 changes: 3 additions & 2 deletions VkNet/Abstractions/Utils/IRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -40,6 +40,7 @@ public interface IRestClient : IDisposable
/// <param name="uri"> Uri </param>
/// <param name="parameters"> Параметры </param>
/// <param name="encoding"></param>
/// <param name="headers"> Заголовки </param>
/// <returns> Строковый результат </returns>
Task<HttpResponse<string>> PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> parameters, Encoding encoding);
Task<HttpResponse<string>> PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> parameters, Encoding encoding, IEnumerable<KeyValuePair<string, string>> headers = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<AuthorizationFormResult> ExecuteAsync(Uri url, IApiAuthParams

FillFormFields(form, authParams);

var response = await _restClient.PostAsync(new(form.Action), form.Fields, Encoding.GetEncoding(1251))
var response = await _restClient.PostAsync(new(form.Action), form.Fields, Encoding.UTF8, form.Headers)
.ConfigureAwait(false);

if (!response.IsSuccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ protected override void FillFormFields(VkHtmlFormResult form, IApiAuthParams aut
{
form.Fields[AuthorizationFormFields.Password] = authParams.Password;
}

form.Headers = new System.Collections.Generic.Dictionary<string, string>
{
{ "content-type", "application/x-www-form-urlencoded" },
{ "origin", "https://oauth.vk.com" },
{ "referer", "https://oauth.vk.com/" }
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public class VkHtmlFormResult
/// Поля формы
/// </summary>
public Dictionary<string, string> Fields { get; set; }

/// <summary>
/// Заголовки для запросов
/// </summary>
public Dictionary<string, string> Headers { get; set; }
}
16 changes: 15 additions & 1 deletion VkNet/Utils/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -50,14 +51,27 @@ public Task<HttpResponse<string>> GetAsync(Uri uri, IEnumerable<KeyValuePair<str
}

/// <inheritdoc />
public Task<HttpResponse<string>> PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> parameters, Encoding encoding)
public Task<HttpResponse<string>> PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> parameters, Encoding encoding, IEnumerable<KeyValuePair<string, string>> headers = null)
{
if (_logger != null)
{
var json = JsonConvert.SerializeObject(parameters);
_logger.LogDebug("POST request: {Uri}{NewLine}{PrettyJson}", uri, Environment.NewLine, Utilities.PrettyPrintJson(json));
}

if (headers != null && headers.Any())
{
headers.ToList().ForEach(header => {
if (header.Key.ToLower() == "content-type")
{
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value));
} else
{
HttpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
});
}

var content = new FormUrlEncodedContent(parameters);

return CallAsync(() => HttpClient.PostAsync(uri, content), encoding);
Expand Down