2015-10-16 23:44:35 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
|
using System;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
2017-06-06 17:56:26 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Auth
|
|
|
|
|
{
|
|
|
|
|
public class ClientCredentialsAuth
|
|
|
|
|
{
|
|
|
|
|
public Scope Scope { get; set; }
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public string ClientId { get; set; }
|
|
|
|
|
public string ClientSecret { get; set; }
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the auth process and
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A new Token</returns>
|
|
|
|
|
public Token DoAuth()
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
wc.Headers.Add("Authorization",
|
|
|
|
|
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret)));
|
|
|
|
|
|
|
|
|
|
NameValueCollection col = new NameValueCollection
|
|
|
|
|
{
|
|
|
|
|
{"grant_type", "client_credentials"},
|
|
|
|
|
{"scope", Scope.GetStringAttribute(" ")}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
byte[] data;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
|
|
|
|
data = Encoding.UTF8.GetBytes(reader.ReadToEnd());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return JsonConvert.DeserializeObject<Token>(Encoding.UTF8.GetString(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-06 17:56:26 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the auth process async and
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A new Token</returns>
|
|
|
|
|
public async Task<Token> DoAuthAsync()
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
wc.Headers.Add("Authorization",
|
|
|
|
|
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret)));
|
|
|
|
|
|
|
|
|
|
NameValueCollection col = new NameValueCollection
|
|
|
|
|
{
|
|
|
|
|
{"grant_type", "client_credentials"},
|
|
|
|
|
{"scope", Scope.GetStringAttribute(" ")}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
byte[] data;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
data = await wc.UploadValuesTaskAsync("https://accounts.spotify.com/api/token", "POST", col);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
|
|
|
|
data = Encoding.UTF8.GetBytes(await reader.ReadToEndAsync());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return JsonConvert.DeserializeObject<Token>(Encoding.UTF8.GetString(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|