Переделан способ авторизации по qr
All checks were successful
Release / pack-and-publish (release) Successful in 1m5s

This commit is contained in:
FrigaT
2026-04-20 14:31:47 +03:00
parent a7caf829d3
commit 0bbaac5689
15 changed files with 194 additions and 50 deletions

View File

@@ -1,4 +1,5 @@
using System.Net;
using System.Net.Http.Headers;
using YandexMusic.API.Models.Account;
using YandexMusic.API.Requests.Common;
@@ -8,7 +9,47 @@ internal class YGetAuthCookiesBuilder : YAuthRequestBuilder<YAccessToken?, objec
{
public YGetAuthCookiesBuilder(YandexMusicApi api) : base(api) { }
protected override string Method => WebRequestMethods.Http.Post;
protected override string BaseUrl => YConstants.Endpoints.MobilePassportUrl;
protected override string PathTemplate => "1/bundle/oauth/token_by_sessionid";
protected override HttpContent? GetContent(object _)
=> new FormUrlEncodedContent(new Dictionary<string, string> { { "client_id", YConstants.XClientId }, { "client_secret", YConstants.XClientSecret } });
protected override void SetCustomHeaders(HttpRequestHeaders headers)
{
base.SetCustomHeaders(headers);
headers.Add("ya-client-host", "passport.yandex.ru");
var cookieString = GetCookieString();
if (!string.IsNullOrEmpty(cookieString))
headers.Add("Ya-Client-Cookie", cookieString);
}
private string GetCookieString()
{
var container = Storage.CookieContainer;
if (container == null) return string.Empty;
var uris = new[]
{
new Uri("https://yandex.ru"),
new Uri("https://passport.yandex.ru"),
new Uri("https://mobileproxy.passport.yandex.net")
};
var cookies = new List<string>();
foreach (var uri in uris)
{
var cookieCollection = container.GetCookies(uri);
foreach (Cookie cookie in cookieCollection)
{
cookies.Add($"{cookie.Name}={cookie.Value}");
}
}
var distinct = cookies
.Select(c => c.Split('=')[0])
.Distinct()
.Select(name => cookies.First(c => c.StartsWith(name + "=")))
.ToList();
return string.Join("; ", distinct);
}
}