diff --git a/ExampleConsoleApp/Program.cs b/ExampleConsoleApp/Program.cs index 9b75439..d403391 100644 --- a/ExampleConsoleApp/Program.cs +++ b/ExampleConsoleApp/Program.cs @@ -14,30 +14,18 @@ public class Program { - private string clientSecret; - private string clientId; - private string redirectUri; - private AuthParameters parameters; + private readonly IConfiguration configuration; + private readonly SpotifyAuthentication spotifyAuthentication; - public Program(IConfiguration configuration) + public Program(IConfiguration configuration, SpotifyAuthentication spotifyAuthentication) { - this.clientId = configuration["Spotify:ClientId"]; - this.clientSecret = configuration["Spotify:ClientSecret"]; - this.redirectUri = configuration["Spotify:RedirectUri"]; - - parameters = new AuthParameters - { - ClientId = this.clientId, - ClientSecret = this.clientSecret, - RedirectUri = this.redirectUri, - Scopes = Scopes.All, - ShowDialog = true - }; + this.configuration = configuration; + this.spotifyAuthentication = spotifyAuthentication; } public async Task Run() { - var token = await this.GetToken(); + var token = await this.spotifyAuthentication.GetToken(); Console.Clear(); Console.WriteLine("Successfully logged in!"); @@ -50,12 +38,8 @@ public async Task Run() var playlists = await api.Playlist.GetMyPlaylists(); Console.WriteLine("Your playlists:"); - foreach (var simplePlaylist in playlists) - { - Console.Write($"{simplePlaylist.Name}, "); - } + Console.WriteLine(string.Join(", ", playlists.Select(x => x.Name))); - Console.WriteLine(); Console.WriteLine(); var devices = await api.Player.GetAvailableDevices(); @@ -65,71 +49,18 @@ public async Task Run() Console.Write($"{device.Name}, "); } - Console.WriteLine("Refreshing token..."); - var newToken = await AuthorizationCode.RefreshTokenAsync(this.parameters, token); - Console.WriteLine($"old: {token.AccessToken}"); - Console.WriteLine($"new: {newToken.AccessToken}"); - - api.SetToken(newToken); - - me = await api.UserProfile.GetMe(); - - Console.WriteLine($"Hello {me.DisplayName}"); + // Console.WriteLine("Refreshing token..."); + // var newToken = await AuthorizationCode.RefreshTokenAsync(this.parameters, token); + // Console.WriteLine($"old: {token.AccessToken}"); + // Console.WriteLine($"new: {newToken.AccessToken}"); + // + // api.SetToken(newToken); + // + // me = await api.UserProfile.GetMe(); + // + // Console.WriteLine($"Hello {me.DisplayName}"); Console.ReadLine(); } - - public async Task GetToken() - { - var state = Guid.NewGuid().ToString(); // Save this state because you must check it later - - - - var url = AuthorizationCode.GetUrl(this.parameters, state); - - Console.WriteLine("Opening url..."); - Console.WriteLine(url); - - var ps = new ProcessStartInfo(url) - { - UseShellExecute = true, - Verb = "open" - }; - Process.Start(ps); - - var queryString = await this.StartServerAndRetrieveAuthCode(); - - // The retrieved callback: - var retrievedState = queryString["state"]; - var retrievedCode = queryString["code"]; - var retrievedError = queryString["error"]; - - if (retrievedError != null) - { - throw new Exception(retrievedError); - } - - if (state != retrievedState) - { - throw new Exception("State did not match!"); - } - - var token = await AuthorizationCode.ProcessCallbackAsync(this.parameters, retrievedCode); - - return token; - } - - public async Task StartServerAndRetrieveAuthCode(string url = "http://localhost:8080/") - { - var listener = new HttpListener(); - listener.Prefixes.Add("http://localhost:8080/"); - listener.Start(); - - var context = await listener.GetContextAsync(); - - listener.Stop(); - - return context.Request.QueryString; - } } } diff --git a/ExampleConsoleApp/SpotifyAuthentication.cs b/ExampleConsoleApp/SpotifyAuthentication.cs new file mode 100644 index 0000000..a86ebc5 --- /dev/null +++ b/ExampleConsoleApp/SpotifyAuthentication.cs @@ -0,0 +1,87 @@ +namespace ExampleConsoleApp +{ + using System; + using System.Collections.Specialized; + using System.Diagnostics; + using System.Net; + using System.Threading.Tasks; + using Microsoft.Extensions.Configuration; + using SpotifyWebApi.Auth; + using SpotifyWebApi.Model.Auth; + using SpotifyWebApi.Model.Enum; + + public class SpotifyAuthentication + { + private string clientSecret; + private string clientId; + private string redirectUri; + private AuthParameters parameters; + + public SpotifyAuthentication(IConfiguration configuration) + { + this.clientId = configuration["Spotify:ClientId"]; + this.clientSecret = configuration["Spotify:ClientSecret"]; + this.redirectUri = configuration["Spotify:RedirectUri"]; + + this.parameters = new AuthParameters + { + ClientId = this.clientId, + ClientSecret = this.clientSecret, + RedirectUri = this.redirectUri, + Scopes = Scopes.All, + ShowDialog = true + }; + } + + public async Task GetToken() + { + var state = Guid.NewGuid().ToString(); // Save this state because you must check it later + + var url = AuthorizationCode.GetUrl(this.parameters, state); + + Console.WriteLine("Opening url..."); + Console.WriteLine(url); + + var ps = new ProcessStartInfo(url) + { + UseShellExecute = true, + Verb = "open" + }; + Process.Start(ps); + + var queryString = await this.StartServerAndRetrieveAuthCode(); + + // The retrieved callback: + var retrievedState = queryString["state"]; + var retrievedCode = queryString["code"]; + var retrievedError = queryString["error"]; + + if (retrievedError != null) + { + throw new Exception(retrievedError); + } + + if (state != retrievedState) + { + throw new Exception("State did not match!"); + } + + var token = await AuthorizationCode.ProcessCallbackAsync(this.parameters, retrievedCode); + + return token; + } + + public async Task StartServerAndRetrieveAuthCode(string url = "http://localhost:8080/") + { + var listener = new HttpListener(); + listener.Prefixes.Add("http://localhost:8080/"); + listener.Start(); + + var context = await listener.GetContextAsync(); + + listener.Stop(); + + return context.Request.QueryString; + } + } +} diff --git a/ExampleConsoleApp/Bootstrap.cs b/ExampleConsoleApp/Startup.cs similarity index 88% rename from ExampleConsoleApp/Bootstrap.cs rename to ExampleConsoleApp/Startup.cs index 8469c75..9aa3612 100644 --- a/ExampleConsoleApp/Bootstrap.cs +++ b/ExampleConsoleApp/Startup.cs @@ -1,14 +1,11 @@ -using System; - -namespace ExampleConsoleApp +namespace ExampleConsoleApp { using System.IO; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - using SpotifyWebApi.Model.Enum; - class Bootstrap + class Startup { static async Task Main(string[] args) { @@ -22,13 +19,13 @@ static async Task Main(string[] args) IConfiguration configuration = builder.Build(); serviceCollection.AddScoped(_ => configuration); + serviceCollection.AddScoped(); serviceCollection.AddScoped(); var serviceProvider = serviceCollection.BuildServiceProvider(); var p = serviceProvider.GetRequiredService(); await p.Run(); - } } } \ No newline at end of file diff --git a/SpotifyWebApi/Business/ApiClient.cs b/SpotifyWebApi/Business/ApiClient.cs index 8db226e..2a3b329 100644 --- a/SpotifyWebApi/Business/ApiClient.cs +++ b/SpotifyWebApi/Business/ApiClient.cs @@ -100,7 +100,7 @@ public static async Task DeleteAsync(Uri uri, Token token = null /// A newly created containing the authentication provided by the token. private static HttpClient MakeHttpClient(Token token = null) { - using var client = new HttpClient(); + var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/x-www-form-urlencoded")); if (token != null)