diff --git a/SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs b/SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs index 49cff8d4..ee8f710e 100644 --- a/SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs +++ b/SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs @@ -16,12 +16,16 @@ namespace SpotifyAPI.Web.Auth public event Func ImplictGrantReceived; private const string CallbackPath = "/"; - private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.DefaultHTML"; + private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets"; + private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site"; private CancellationTokenSource _cancelTokenSource; private readonly WebServer _webServer; - public EmbedIOAuthServer(Uri baseUri, int port, string resourcePath = DefaultResourcePath) + public EmbedIOAuthServer(Uri baseUri, int port) + : this(baseUri, port, Assembly.GetExecutingAssembly(), DefaultResourcePath) { } + + public EmbedIOAuthServer(Uri baseUri, int port, Assembly resourceAssembly, string resourcePath) { Ensure.ArgumentNotNull(baseUri, nameof(baseUri)); @@ -57,7 +61,8 @@ namespace SpotifyAPI.Web.Auth return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8); })) - .WithEmbeddedResources("/", Assembly.GetExecutingAssembly(), resourcePath); + .WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath) + .WithEmbeddedResources("/", resourceAssembly, resourcePath); } public Uri BaseUri { get; } diff --git a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/main.js b/SpotifyAPI.Web.Auth/Resources/auth_assets/main.js similarity index 100% rename from SpotifyAPI.Web.Auth/Resources/DefaultHTML/main.js rename to SpotifyAPI.Web.Auth/Resources/auth_assets/main.js diff --git a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/favicon.ico b/SpotifyAPI.Web.Auth/Resources/default_site/favicon.ico similarity index 100% rename from SpotifyAPI.Web.Auth/Resources/DefaultHTML/favicon.ico rename to SpotifyAPI.Web.Auth/Resources/default_site/favicon.ico diff --git a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/index.html b/SpotifyAPI.Web.Auth/Resources/default_site/index.html similarity index 96% rename from SpotifyAPI.Web.Auth/Resources/DefaultHTML/index.html rename to SpotifyAPI.Web.Auth/Resources/default_site/index.html index 211f930d..c861954c 100644 --- a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/index.html +++ b/SpotifyAPI.Web.Auth/Resources/default_site/index.html @@ -8,7 +8,7 @@ - + diff --git a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/logo.svg b/SpotifyAPI.Web.Auth/Resources/default_site/logo.svg similarity index 100% rename from SpotifyAPI.Web.Auth/Resources/DefaultHTML/logo.svg rename to SpotifyAPI.Web.Auth/Resources/default_site/logo.svg diff --git a/SpotifyAPI.Web.Auth/Resources/DefaultHTML/main.css b/SpotifyAPI.Web.Auth/Resources/default_site/main.css similarity index 100% rename from SpotifyAPI.Web.Auth/Resources/DefaultHTML/main.css rename to SpotifyAPI.Web.Auth/Resources/default_site/main.css diff --git a/SpotifyAPI.Web.Examples/CLI.CustomHTML/CLI.CustomHTML.csproj b/SpotifyAPI.Web.Examples/CLI.CustomHTML/CLI.CustomHTML.csproj new file mode 100644 index 00000000..53f289a9 --- /dev/null +++ b/SpotifyAPI.Web.Examples/CLI.CustomHTML/CLI.CustomHTML.csproj @@ -0,0 +1,17 @@ + + + + + + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/SpotifyAPI.Web.Examples/CLI.CustomHTML/Program.cs b/SpotifyAPI.Web.Examples/CLI.CustomHTML/Program.cs new file mode 100644 index 00000000..460ef6d6 --- /dev/null +++ b/SpotifyAPI.Web.Examples/CLI.CustomHTML/Program.cs @@ -0,0 +1,59 @@ +using System.Reflection; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using SpotifyAPI.Web; +using SpotifyAPI.Web.Auth; +using static SpotifyAPI.Web.Scopes; + +namespace CLI.CustomHTML +{ + public class Program + { + private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID"); + private static readonly string clientSecret = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_SECRET"); + private static EmbedIOAuthServer _server; + + public static async Task Main() + { + _server = new EmbedIOAuthServer( + new Uri("http://localhost:5000"), 5000, Assembly.GetExecutingAssembly(), "CLI.CustomHTML.Resources.custom_site"); + await _server.Start(); + + _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived; + + var request = new LoginRequest(clientId, LoginRequest.ResponseType.Code) + { + Scope = new List { UserReadEmail } + }; + + Uri url = _server.BuildLoginUri(request); + try + { + BrowserUtil.Open(url); + } + catch (Exception) + { + Console.WriteLine("Unable to open URL, manually open: {0}", url); + } + + Console.ReadKey(); + } + + private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response) + { + await _server.Stop(); + + AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken( + new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.RedirectUri) + ); + + var config = SpotifyClientConfig.CreateDefault().WithToken(token.AccessToken, token.TokenType); + var spotify = new SpotifyClient(config); + + var me = await spotify.UserProfile.Current(); + + Console.WriteLine($"Your E-Mail: {me.Email}"); + } + } +} diff --git a/SpotifyAPI.Web.Examples/CLI.CustomHTML/Resources/custom_site/index.html b/SpotifyAPI.Web.Examples/CLI.CustomHTML/Resources/custom_site/index.html new file mode 100644 index 00000000..bacda307 --- /dev/null +++ b/SpotifyAPI.Web.Examples/CLI.CustomHTML/Resources/custom_site/index.html @@ -0,0 +1,83 @@ + + + + + + + Custom Title + + + + + + + +

+ Authentication successful +

+

+ Just an example how to load custom HTML! +

+ + + + + diff --git a/SpotifyAPI.Web.Examples/CLI/CLI.csproj b/SpotifyAPI.Web.Examples/CLI.PersistentConfig/CLI.PersistentConfig.csproj similarity index 100% rename from SpotifyAPI.Web.Examples/CLI/CLI.csproj rename to SpotifyAPI.Web.Examples/CLI.PersistentConfig/CLI.PersistentConfig.csproj index 9f7c5896..ddfa049b 100644 --- a/SpotifyAPI.Web.Examples/CLI/CLI.csproj +++ b/SpotifyAPI.Web.Examples/CLI.PersistentConfig/CLI.PersistentConfig.csproj @@ -1,17 +1,17 @@ - - - - - - - - - Exe netcoreapp3.1 + + + + + + + + + diff --git a/SpotifyAPI.Web.Examples/CLI/Program.cs b/SpotifyAPI.Web.Examples/CLI.PersistentConfig/Program.cs similarity index 99% rename from SpotifyAPI.Web.Examples/CLI/Program.cs rename to SpotifyAPI.Web.Examples/CLI.PersistentConfig/Program.cs index 07a79a66..d7fc9288 100644 --- a/SpotifyAPI.Web.Examples/CLI/Program.cs +++ b/SpotifyAPI.Web.Examples/CLI.PersistentConfig/Program.cs @@ -9,7 +9,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using static SpotifyAPI.Web.Scopes; -namespace CLI +namespace CLI.PersistentConfig { /// /// This is a basic example how to get user access using the Auth package and a CLI Program diff --git a/SpotifyAPI.Web.Examples/CLI/.gitignore b/SpotifyAPI.Web.Examples/CLI/.gitignore deleted file mode 100644 index 83f6e397..00000000 --- a/SpotifyAPI.Web.Examples/CLI/.gitignore +++ /dev/null @@ -1 +0,0 @@ -credentials.json diff --git a/SpotifyAPI.sln b/SpotifyAPI.sln index 37fe00ce..a34d48ae 100644 --- a/SpotifyAPI.sln +++ b/SpotifyAPI.sln @@ -11,7 +11,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpotifyAPI.Web.Auth", "Spot EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SpotifyAPI.Web.Examples", "SpotifyAPI.Web.Examples", "{48A7DE65-29BB-409C-AC45-77F6586C0B15}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLI", "SpotifyAPI.Web.Examples\CLI\CLI.csproj", "{F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLI.PersistentConfig", "SpotifyAPI.Web.Examples\CLI.PersistentConfig\CLI.PersistentConfig.csproj", "{F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLI.CustomHTML", "SpotifyAPI.Web.Examples\CLI.CustomHTML\CLI.CustomHTML.csproj", "{941AB88D-B3A9-407F-BF88-BFE14B401687}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,6 +37,10 @@ Global {F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}.Debug|Any CPU.Build.0 = Debug|Any CPU {F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}.Release|Any CPU.ActiveCfg = Release|Any CPU {F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}.Release|Any CPU.Build.0 = Release|Any CPU + {941AB88D-B3A9-407F-BF88-BFE14B401687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {941AB88D-B3A9-407F-BF88-BFE14B401687}.Debug|Any CPU.Build.0 = Debug|Any CPU + {941AB88D-B3A9-407F-BF88-BFE14B401687}.Release|Any CPU.ActiveCfg = Release|Any CPU + {941AB88D-B3A9-407F-BF88-BFE14B401687}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -44,5 +50,6 @@ Global EndGlobalSection GlobalSection(NestedProjects) = preSolution {F4ECE937-99F2-4C4F-9F5C-4AB875D9538A} = {48A7DE65-29BB-409C-AC45-77F6586C0B15} + {941AB88D-B3A9-407F-BF88-BFE14B401687} = {48A7DE65-29BB-409C-AC45-77F6586C0B15} EndGlobalSection EndGlobal