2020-03-09 19:47:39 +00:00
|
|
|
using System;
|
2018-08-24 13:10:13 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
{
|
2020-03-09 19:47:39 +00:00
|
|
|
public class CredentialsAuth
|
|
|
|
{
|
|
|
|
public string ClientSecret { get; set; }
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public string ClientId { get; set; }
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public ProxyConfig ProxyConfig { get; set; }
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
public CredentialsAuth(string clientId, string clientSecret)
|
|
|
|
{
|
|
|
|
ClientId = clientId;
|
|
|
|
ClientSecret = clientSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Token> GetToken()
|
|
|
|
{
|
|
|
|
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret));
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>
|
|
|
|
{new KeyValuePair<string, string>("grant_type", "client_credentials")
|
|
|
|
};
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
HttpClientHandler handler = ProxyConfig.CreateClientHandler(ProxyConfig);
|
|
|
|
HttpClient client = new HttpClient(handler);
|
|
|
|
client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");
|
|
|
|
HttpContent content = new FormUrlEncodedContent(args);
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content);
|
|
|
|
string msg = await resp.Content.ReadAsStringAsync();
|
2018-08-24 13:10:13 +01:00
|
|
|
|
2020-03-09 19:47:39 +00:00
|
|
|
return JsonConvert.DeserializeObject<Token>(msg);
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|
2020-03-09 19:47:39 +00:00
|
|
|
}
|
2018-08-24 13:10:13 +01:00
|
|
|
}
|