Added path in examples

This commit is contained in:
Jonas Dellinger 2020-05-15 22:02:54 +02:00
parent 7f90dffe79
commit f05b208f6a
8 changed files with 16 additions and 15 deletions

View File

@ -15,7 +15,6 @@ namespace SpotifyAPI.Web.Auth
public event Func<object, AuthorizationCodeResponse, Task> AuthorizationCodeReceived; public event Func<object, AuthorizationCodeResponse, Task> AuthorizationCodeReceived;
public event Func<object, ImplictGrantResponse, Task> ImplictGrantReceived; public event Func<object, ImplictGrantResponse, Task> ImplictGrantReceived;
private const string CallbackPath = "/";
private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets"; private const string AssetsResourcePath = "SpotifyAPI.Web.Auth.Resources.auth_assets";
private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site"; private const string DefaultResourcePath = "SpotifyAPI.Web.Auth.Resources.default_site";
@ -62,11 +61,10 @@ namespace SpotifyAPI.Web.Auth
return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8); return ctx.SendStringAsync("OK", "text/plain", Encoding.UTF8);
})) }))
.WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath) .WithEmbeddedResources("/auth_assets", Assembly.GetExecutingAssembly(), AssetsResourcePath)
.WithEmbeddedResources("/", resourceAssembly, resourcePath); .WithEmbeddedResources(baseUri.AbsolutePath, resourceAssembly, resourcePath);
} }
public Uri BaseUri { get; } public Uri BaseUri { get; }
public Uri RedirectUri { get => new Uri(BaseUri, CallbackPath); }
public int Port { get; } public int Port { get; }
public Task Start() public Task Start()
@ -86,12 +84,10 @@ namespace SpotifyAPI.Web.Auth
{ {
Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(request, nameof(request));
var callbackUri = new Uri(BaseUri, CallbackPath);
StringBuilder builder = new StringBuilder(SpotifyUrls.Authorize.ToString()); StringBuilder builder = new StringBuilder(SpotifyUrls.Authorize.ToString());
builder.Append($"?client_id={request.ClientId}"); builder.Append($"?client_id={request.ClientId}");
builder.Append($"&response_type={request.ResponseTypeParam.ToString().ToLower()}"); builder.Append($"&response_type={request.ResponseTypeParam.ToString().ToLower()}");
builder.Append($"&redirect_uri={HttpUtility.UrlEncode(callbackUri.ToString())}"); builder.Append($"&redirect_uri={HttpUtility.UrlEncode(BaseUri.ToString())}");
if (!string.IsNullOrEmpty(request.State)) if (!string.IsNullOrEmpty(request.State))
{ {
builder.Append($"&state={HttpUtility.UrlEncode(request.State)}"); builder.Append($"&state={HttpUtility.UrlEncode(request.State)}");

View File

@ -14,6 +14,6 @@ namespace SpotifyAPI.Web.Auth
Uri BuildLoginUri(LoginRequest request); Uri BuildLoginUri(LoginRequest request);
Uri RedirectUri { get; } Uri BaseUri { get; }
} }
} }

View File

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -7,7 +7,7 @@
<title>Spotify Authorization</title> <title>Spotify Authorization</title>
<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="/auth_assets/main.css" rel="stylesheet">
<script src="/auth_assets/main.js"></script> <script src="/auth_assets/main.js"></script>
</head> </head>
@ -15,7 +15,7 @@
<main> <main>
<div class="flex justify-center flex-wrap logo"> <div class="flex justify-center flex-wrap logo">
<div class="w-1/8"> <div class="w-1/8">
<img src="logo.svg" width="120" height="120" /> <img src="/auth_assets/logo.svg" width="120" height="120" />
</div> </div>
</div> </div>
<h1 class="text-4xl">Success!</h1> <h1 class="text-4xl">Success!</h1>

View File

@ -17,7 +17,11 @@ namespace CLI.CustomHTML
public static async Task Main() public static async Task Main()
{ {
_server = new EmbedIOAuthServer( _server = new EmbedIOAuthServer(
new Uri("http://localhost:5000"), 5000, Assembly.GetExecutingAssembly(), "CLI.CustomHTML.Resources.custom_site"); new Uri("http://localhost:5000/callback"),
5000,
Assembly.GetExecutingAssembly(),
"CLI.CustomHTML.Resources.custom_site"
);
await _server.Start(); await _server.Start();
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived; _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
@ -45,7 +49,7 @@ namespace CLI.CustomHTML
await _server.Stop(); await _server.Stop();
AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken( AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken(
new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.RedirectUri) new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.BaseUri)
); );
var config = SpotifyClientConfig.CreateDefault().WithToken(token.AccessToken, token.TokenType); var config = SpotifyClientConfig.CreateDefault().WithToken(token.AccessToken, token.TokenType);
@ -54,6 +58,7 @@ namespace CLI.CustomHTML
var me = await spotify.UserProfile.Current(); var me = await spotify.UserProfile.Current();
Console.WriteLine($"Your E-Mail: {me.Email}"); Console.WriteLine($"Your E-Mail: {me.Email}");
Environment.Exit(0);
} }
} }
} }

View File

@ -23,9 +23,9 @@
<h1> <h1>
Authentication successful Authentication successful
</h1> </h1>
<p> <a href="https://bl.ocks.org/mbostock/9539958">
Just an example how to load custom HTML! Just an example how to load custom HTML!
</p> </a>
</body> </body>
<script> <script>

View File

@ -61,7 +61,7 @@ namespace CLI.PersistentConfig
private static async Task StartAuthentication() private static async Task StartAuthentication()
{ {
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000"), 5000); _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
await _server.Start(); await _server.Start();
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived; _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
@ -85,7 +85,7 @@ namespace CLI.PersistentConfig
{ {
await _server.Stop(); await _server.Stop();
AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken( AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken(
new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.RedirectUri) new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.BaseUri)
); );
await File.WriteAllTextAsync(CredentialsPath, JsonConvert.SerializeObject(token)); await File.WriteAllTextAsync(CredentialsPath, JsonConvert.SerializeObject(token));