44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
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.Spotify;
|
|
|
|
namespace SWAD.API.Services.MusicAPI.Auth;
|
|
|
|
/// <inheritdoc />
|
|
public class SpotifyAuthService : AbstractAuthService
|
|
{
|
|
public SpotifyAuthService(IOptions<ApiServicesConfig> data)
|
|
{
|
|
ServiceType = MusicService.Spotify;
|
|
Data = data.Value.ServicesData.First(x => x.Name == ServiceType.ToString());
|
|
}
|
|
|
|
public override async Task<IAuthResponse?> GetToken()
|
|
{
|
|
using var httpClient = new HttpClient();
|
|
var content = new Dictionary<string, string>
|
|
{
|
|
{ "grant_type", "client_credentials" },
|
|
{ "client_id", Data.ClientId },
|
|
{ "client_secret", Data.Secret }
|
|
};
|
|
try
|
|
{
|
|
var response = await httpClient.PostAsync(Data.Endpoints.Token, new FormUrlEncodedContent(content));
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var ans = await JsonSerializer.DeserializeAsync<SpotifyAuthResponse>(
|
|
await response.Content.ReadAsStreamAsync());
|
|
return ans;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |