chore: init commit from GitHub
This commit is contained in:
46
SWAD.API/Services/MusicAPI/Auth/TidalAuthService.cs
Normal file
46
SWAD.API/Services/MusicAPI/Auth/TidalAuthService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SWAD.API.Consts.Enums;
|
||||
using SWAD.API.Models.Config.ApiServices;
|
||||
using SWAD.API.Models.JsonStructures.MusicAPI.Tidal;
|
||||
|
||||
namespace SWAD.API.Services.MusicAPI.Auth;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class TidalAuthService : AbstractAuthService
|
||||
{
|
||||
public TidalAuthService(IOptions<ApiServicesConfig> data)
|
||||
{
|
||||
ServiceType = MusicService.Tidal;
|
||||
Data = data.Value.ServicesData.First(x => x.Name == ServiceType.ToString());
|
||||
}
|
||||
|
||||
public override async Task<IAuthResponse?> GetToken()
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
var base64Token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Data.ClientId}:{Data.Secret}"));
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
|
||||
var content = new Dictionary<string, string>
|
||||
{
|
||||
{ "grant_type", "client_credentials" }
|
||||
};
|
||||
try
|
||||
{
|
||||
var response = await httpClient.PostAsync(Data.Endpoints.Token, new FormUrlEncodedContent(content));
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var ans = await JsonSerializer.DeserializeAsync<TidalAuthResponse>(
|
||||
await response.Content.ReadAsStreamAsync());
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user