mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 06:56:27 +00:00
Async auth support for ClientCredentialsAuth (#148)
This commit is contained in:
parent
a932aaadc8
commit
512d3d76ad
@ -6,6 +6,7 @@ using System.Collections.Specialized;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SpotifyAPI.Web.Auth
|
||||
{
|
||||
@ -47,5 +48,38 @@ namespace SpotifyAPI.Web.Auth
|
||||
return JsonConvert.DeserializeObject<Token>(Encoding.UTF8.GetString(data));
|
||||
}
|
||||
}
|
||||
|
||||
/// <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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user