Added an example for custom HTML

This commit is contained in:
Jonas Dellinger 2020-05-15 21:41:53 +02:00
parent 9431bb44b5
commit 498c426ecd
13 changed files with 186 additions and 16 deletions

View File

@ -16,12 +16,16 @@ namespace SpotifyAPI.Web.Auth
public event Func<object, ImplictGrantResponse, Task> ImplictGrantReceived; public event Func<object, ImplictGrantResponse, Task> ImplictGrantReceived;
private const string CallbackPath = "/"; 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 CancellationTokenSource _cancelTokenSource;
private readonly WebServer _webServer; 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)); Ensure.ArgumentNotNull(baseUri, nameof(baseUri));
@ -57,7 +61,8 @@ namespace SpotifyAPI.Web.Auth
return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8); 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; } public Uri BaseUri { get; }

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -8,7 +8,7 @@
<meta name='viewport' content='width=device-width, initial-scale=1'> <meta name='viewport' content='width=device-width, initial-scale=1'>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet"> <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<link href="/main.css" rel="stylesheet"> <link href="/main.css" rel="stylesheet">
<script src="/main.js"></script> <script src="/auth_assets/main.js"></script>
</head> </head>
<body> <body>

View File

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\**\*" />
</ItemGroup>
</Project>

View File

@ -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<string> { 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}");
}
}
}

View File

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Custom Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>
body {
text-align: center;
color: white;
margin: 0;
background: #111;
min-width: 960px;
}
</style>
<script src='/auth_assets/main.js'></script>
<script src="//d3js.org/d3.v3.min.js"></script>
</head>
<body>
<h1>
Authentication successful
</h1>
<p>
Just an example how to load custom HTML!
</p>
</body>
<script>
var width = Math.max(960, innerWidth),
height = Math.max(500, innerHeight);
var x1 = width / 2,
y1 = height / 2,
x0 = x1,
y0 = y1,
i = 0,
r = 200,
τ = 2 * Math.PI;
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height)
.on("ontouchstart" in document ? "touchmove" : "mousemove", move);
var context = canvas.node().getContext("2d");
context.globalCompositeOperation = "lighter";
context.lineWidth = 2;
d3.timer(function () {
context.clearRect(0, 0, width, height);
var z = d3.hsl(++i % 360, 1, .5).rgb(),
c = "rgba(" + z.r + "," + z.g + "," + z.b + ",",
x = x0 += (x1 - x0) * .1,
y = y0 += (y1 - y0) * .1;
d3.select({}).transition()
.duration(2000)
.ease(Math.sqrt)
.tween("circle", function () {
return function (t) {
context.strokeStyle = c + (1 - t) + ")";
context.beginPath();
context.arc(x, y, r * t, 0, τ);
context.stroke();
};
});
});
function move() {
var mouse = d3.mouse(this);
x1 = mouse[0];
y1 = mouse[1];
d3.event.preventDefault();
}
</script>
</html>

View File

@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -9,7 +9,7 @@ using System.Collections.Generic;
using Newtonsoft.Json; using Newtonsoft.Json;
using static SpotifyAPI.Web.Scopes; using static SpotifyAPI.Web.Scopes;
namespace CLI namespace CLI.PersistentConfig
{ {
/// <summary> /// <summary>
/// This is a basic example how to get user access using the Auth package and a CLI Program /// This is a basic example how to get user access using the Auth package and a CLI Program

View File

@ -1 +0,0 @@
credentials.json

View File

@ -11,7 +11,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpotifyAPI.Web.Auth", "Spot
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SpotifyAPI.Web.Examples", "SpotifyAPI.Web.Examples", "{48A7DE65-29BB-409C-AC45-77F6586C0B15}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SpotifyAPI.Web.Examples", "SpotifyAPI.Web.Examples", "{48A7DE65-29BB-409C-AC45-77F6586C0B15}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{F4ECE937-99F2-4C4F-9F5C-4AB875D9538A}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -44,5 +50,6 @@ Global
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{F4ECE937-99F2-4C4F-9F5C-4AB875D9538A} = {48A7DE65-29BB-409C-AC45-77F6586C0B15} {F4ECE937-99F2-4C4F-9F5C-4AB875D9538A} = {48A7DE65-29BB-409C-AC45-77F6586C0B15}
{941AB88D-B3A9-407F-BF88-BFE14B401687} = {48A7DE65-29BB-409C-AC45-77F6586C0B15}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal