55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using YandexMusic.API.Models.Account;
|
|
using YandexMusic.API.Requests.Common;
|
|
|
|
namespace YandexMusic.API.Requests.Account;
|
|
|
|
internal class YGetAuthCookiesBuilder : YAuthRequestBuilder<YAccessToken?, object>
|
|
{
|
|
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);
|
|
}
|
|
} |