From e7f40474c27b0b6b4ab09168f4b8b345a4b8629e Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Fri, 21 Sep 2018 14:45:14 +0200 Subject: [PATCH] Added method to RefreshToken for auth code flow Fixed some includes --- SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs | 34 +++++++++++++++----- SpotifyAPI.Web.Auth/SpotifyAuthServer.cs | 2 +- SpotifyAPI.Web.Example/Program.cs | 16 ++++----- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs index ac531b34..fa9cf800 100644 --- a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs +++ b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; @@ -9,11 +10,8 @@ using SpotifyAPI.Web.Models; using Unosquare.Labs.EmbedIO; using Unosquare.Labs.EmbedIO.Constants; using Unosquare.Labs.EmbedIO.Modules; -#if NETSTANDARD2_0 -using System.Net.Http; -#endif +using Unosquare.Swan; #if NET46 -using System.Net.Http; using HttpListenerContext = Unosquare.Net.HttpListenerContext; #endif @@ -45,12 +43,32 @@ namespace SpotifyAPI.Web.Auth return ShouldRegisterNewApp() ? $"{RedirectUri}/start.html#{State}" : base.GetUri(); } - protected override WebServer AdaptWebServer(WebServer webServer) => webServer.WithWebApiController(); + protected override WebServer AdaptWebServer(WebServer webServer) + { + return webServer.WithWebApiController(); + } + private string GetAuthHeader() => $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + SecretId))}"; + + public async Task RefreshToken(string refreshToken) + { + List> args = new List>() + { + new KeyValuePair("grant_type", "refresh_token"), + new KeyValuePair("refresh_token", refreshToken) + }; + + HttpClient client = new HttpClient(); + client.DefaultRequestHeaders.Add("Authorization", GetAuthHeader()); + HttpContent content = new FormUrlEncodedContent(args); + + HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content); + string msg = await resp.Content.ReadAsStringAsync(); + + return JsonConvert.DeserializeObject(msg); + } public async Task ExchangeCode(string code) { - string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + SecretId)); - List> args = new List>() { new KeyValuePair("grant_type", "authorization_code"), @@ -59,7 +77,7 @@ namespace SpotifyAPI.Web.Auth }; HttpClient client = new HttpClient(); - client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}"); + client.DefaultRequestHeaders.Add("Authorization", GetAuthHeader()); HttpContent content = new FormUrlEncodedContent(args); HttpResponseMessage resp = await client.PostAsync("https://accounts.spotify.com/api/token", content); diff --git a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs index 81eca9e3..0925ccfa 100644 --- a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs +++ b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs @@ -47,7 +47,7 @@ namespace SpotifyAPI.Web.Auth _server = WebServer.Create(ServerUri, RoutingStrategy.Regex); _server.RegisterModule(new WebApiModule()); AdaptWebServer(_server); - _server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}")); + _server.RegisterModule(new ResourceFilesModule(typeof(T).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}")); #pragma warning disable 4014 _server.RunAsync(_serverSource.Token); #pragma warning restore 4014 diff --git a/SpotifyAPI.Web.Example/Program.cs b/SpotifyAPI.Web.Example/Program.cs index 0aa144e4..bf3d6130 100644 --- a/SpotifyAPI.Web.Example/Program.cs +++ b/SpotifyAPI.Web.Example/Program.cs @@ -6,7 +6,7 @@ using SpotifyAPI.Web.Models; namespace SpotifyAPI.Web.Example { - static class Program + internal static class Program { private static string _clientId = ""; //""; private static string _secretId = ""; //""; @@ -14,11 +14,11 @@ namespace SpotifyAPI.Web.Example static void Main(string[] args) { _clientId = string.IsNullOrEmpty(_clientId) - ? System.Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID") + ? Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID") : _clientId; _secretId = string.IsNullOrEmpty(_secretId) - ? System.Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID") + ? Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID") : _secretId; Console.WriteLine("####### Spotify API Example #######"); @@ -34,10 +34,6 @@ namespace SpotifyAPI.Web.Example auth.Start(); auth.OpenBrowser(); -/* ImplictGrantAuth auth = new ImplictGrantAuth("26d287105e31491889f3cd293d85bfea", "http://localhost:4002", "http://localhost:4002"); - auth.Start(); - auth.OpenBrowser();*/ - Console.ReadLine(); auth.Stop(0); } @@ -48,15 +44,15 @@ namespace SpotifyAPI.Web.Example auth.Stop(); Token token = await auth.ExchangeCode(payload.Code); - SpotifyWebAPI api = new SpotifyWebAPI() + SpotifyWebAPI api = new SpotifyWebAPI { AccessToken = token.AccessToken, TokenType = token.TokenType }; - PrintUsefulDate(api); + PrintUsefulData(api); } - private static async void PrintUsefulDate(SpotifyWebAPI api) + private static async void PrintUsefulData(SpotifyWebAPI api) { PrivateProfile profile = await api.GetPrivateProfileAsync(); string name = string.IsNullOrEmpty(profile.DisplayName) ? profile.Id : profile.DisplayName;