Skip to content

Commit 498c426

Browse files
committed
Added an example for custom HTML
1 parent 9431bb4 commit 498c426

13 files changed

Lines changed: 185 additions & 15 deletions

File tree

‎SpotifyAPI.Web.Auth/EmbedIOAuthServer.cs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public class EmbedIOAuthServer : IAuthServer
1616
public event Func<object, ImplictGrantResponse, Task> ImplictGrantReceived;
1717

1818
private const string CallbackPath = "/";
19-
private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.DefaultHTML";
19+
private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets";
20+
private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site";
2021

2122
private CancellationTokenSource _cancelTokenSource;
2223
private readonly WebServer _webServer;
2324

24-
public EmbedIOAuthServer(Uri baseUri, int port, string resourcePath = DefaultResourcePath)
25+
public EmbedIOAuthServer(Uri baseUri, int port)
26+
: this(baseUri, port, Assembly.GetExecutingAssembly(), DefaultResourcePath) { }
27+
28+
public EmbedIOAuthServer(Uri baseUri, int port, Assembly resourceAssembly, string resourcePath)
2529
{
2630
Ensure.ArgumentNotNull(baseUri, nameof(baseUri));
2731

@@ -57,7 +61,8 @@ public EmbedIOAuthServer(Uri baseUri, int port, string resourcePath = DefaultRes
5761

5862
return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8);
5963
}))
60-
.WithEmbeddedResources("/", Assembly.GetExecutingAssembly(), resourcePath);
64+
.WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath)
65+
.WithEmbeddedResources("/", resourceAssembly, resourcePath);
6166
}
6267

6368
public Uri BaseUri { get; }
File renamed without changes.

SpotifyAPI.Web.Auth/Resources/DefaultHTML/favicon.ico renamed to SpotifyAPI.Web.Auth/Resources/default_site/favicon.ico

File renamed without changes.

SpotifyAPI.Web.Auth/Resources/DefaultHTML/index.html renamed to SpotifyAPI.Web.Auth/Resources/default_site/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta name='viewport' content='width=device-width, initial-scale=1'>
99
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
1010
<link href="/main.css" rel="stylesheet">
11-
<script src="/main.js"></script>
11+
<script src="/auth_assets/main.js"></script>
1212
</head>
1313

1414
<body>
File renamed without changes.
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
5+
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
6+
</ItemGroup>
7+
8+
<PropertyGroup>
9+
<OutputType>Exe</OutputType>
10+
<TargetFramework>netcoreapp3.1</TargetFramework>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<EmbeddedResource Include="Resources\**\*" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Reflection;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
using SpotifyAPI.Web;
6+
using SpotifyAPI.Web.Auth;
7+
using static SpotifyAPI.Web.Scopes;
8+
9+
namespace CLI.CustomHTML
10+
{
11+
public class Program
12+
{
13+
private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
14+
private static readonly string clientSecret = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_SECRET");
15+
private static EmbedIOAuthServer _server;
16+
17+
public static async Task Main()
18+
{
19+
_server = new EmbedIOAuthServer(
20+
new Uri("http://localhost:5000"), 5000, Assembly.GetExecutingAssembly(), "CLI.CustomHTML.Resources.custom_site");
21+
await _server.Start();
22+
23+
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
24+
25+
var request = new LoginRequest(clientId, LoginRequest.ResponseType.Code)
26+
{
27+
Scope = new List<string> { UserReadEmail }
28+
};
29+
30+
Uri url = _server.BuildLoginUri(request);
31+
try
32+
{
33+
BrowserUtil.Open(url);
34+
}
35+
catch (Exception)
36+
{
37+
Console.WriteLine("Unable to open URL, manually open: {0}", url);
38+
}
39+
40+
Console.ReadKey();
41+
}
42+
43+
private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
44+
{
45+
await _server.Stop();
46+
47+
AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken(
48+
new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.RedirectUri)
49+
);
50+
51+
var config = SpotifyClientConfig.CreateDefault().WithToken(token.AccessToken, token.TokenType);
52+
var spotify = new SpotifyClient(config);
53+
54+
var me = await spotify.UserProfile.Current();
55+
56+
Console.WriteLine($"Your E-Mail: {me.Email}");
57+
}
58+
}
59+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset='utf-8'>
6+
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
7+
<title>Custom Title</title>
8+
<meta name='viewport' content='width=device-width, initial-scale=1'>
9+
<style>
10+
body {
11+
text-align: center;
12+
color: white;
13+
margin: 0;
14+
background: #111;
15+
min-width: 960px;
16+
}
17+
</style>
18+
<script src='/auth_assets/main.js'></script>
19+
<script src="//d3js.org/d3.v3.min.js"></script>
20+
</head>
21+
22+
<body>
23+
<h1>
24+
Authentication successful
25+
</h1>
26+
<p>
27+
Just an example how to load custom HTML!
28+
</p>
29+
</body>
30+
31+
<script>
32+
33+
var width = Math.max(960, innerWidth),
34+
height = Math.max(500, innerHeight);
35+
36+
var x1 = width / 2,
37+
y1 = height / 2,
38+
x0 = x1,
39+
y0 = y1,
40+
i = 0,
41+
r = 200,
42+
τ = 2 * Math.PI;
43+
44+
var canvas = d3.select("body").append("canvas")
45+
.attr("width", width)
46+
.attr("height", height)
47+
.on("ontouchstart" in document ? "touchmove" : "mousemove", move);
48+
49+
var context = canvas.node().getContext("2d");
50+
context.globalCompositeOperation = "lighter";
51+
context.lineWidth = 2;
52+
53+
d3.timer(function () {
54+
context.clearRect(0, 0, width, height);
55+
56+
var z = d3.hsl(++i % 360, 1, .5).rgb(),
57+
c = "rgba(" + z.r + "," + z.g + "," + z.b + ",",
58+
x = x0 += (x1 - x0) * .1,
59+
y = y0 += (y1 - y0) * .1;
60+
61+
d3.select({}).transition()
62+
.duration(2000)
63+
.ease(Math.sqrt)
64+
.tween("circle", function () {
65+
return function (t) {
66+
context.strokeStyle = c + (1 - t) + ")";
67+
context.beginPath();
68+
context.arc(x, y, r * t, 0, τ);
69+
context.stroke();
70+
};
71+
});
72+
});
73+
74+
function move() {
75+
var mouse = d3.mouse(this);
76+
x1 = mouse[0];
77+
y1 = mouse[1];
78+
d3.event.preventDefault();
79+
}
80+
81+
</script>
82+
83+
</html>

SpotifyAPI.Web.Examples/CLI/CLI.csproj renamed to SpotifyAPI.Web.Examples/CLI.PersistentConfig/CLI.PersistentConfig.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<ItemGroup>
4-
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
5-
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
6-
</ItemGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
77

88
<ItemGroup>
99
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1010
</ItemGroup>
1111

12-
<PropertyGroup>
13-
<OutputType>Exe</OutputType>
14-
<TargetFramework>netcoreapp3.1</TargetFramework>
15-
</PropertyGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
14+
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
15+
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)