From ae55ddb39b988712ec67172bd0d1ca8c0af14d39 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Sat, 27 May 2023 21:42:31 +0200 Subject: [PATCH] Replace port 5000 (often used) with 5543, fixes #868 --- SpotifyAPI.Docs/docs/5_to_6.md | 2 +- SpotifyAPI.Docs/docs/authorization_code.md | 18 ++++---- SpotifyAPI.Docs/docs/example_asp.md | 4 +- SpotifyAPI.Docs/docs/example_blazor.md | 4 +- SpotifyAPI.Docs/docs/example_blazor_wasm.md | 4 +- .../docs/example_cli_custom_html.md | 2 +- .../docs/example_cli_persistent_config.md | 2 +- SpotifyAPI.Docs/docs/implicit_grant.md | 12 ++--- SpotifyAPI.Docs/docs/pkce.md | 10 ++-- .../Example.CLI.CustomHTML/Program.cs | 10 ++-- .../Example.CLI.PersistentConfig/Program.cs | 17 ++++--- .../Example.TokenSwap/Client/Program.cs | 10 ++-- .../Example.TokenSwap/Server/index.js | 46 +++++++++++-------- 13 files changed, 76 insertions(+), 65 deletions(-) diff --git a/SpotifyAPI.Docs/docs/5_to_6.md b/SpotifyAPI.Docs/docs/5_to_6.md index 721f2e01..883461d6 100644 --- a/SpotifyAPI.Docs/docs/5_to_6.md +++ b/SpotifyAPI.Docs/docs/5_to_6.md @@ -132,7 +132,7 @@ private static async void AuthOnAuthReceived(object sender, AuthorizationCode pa // NEW var config = SpotifyClientConfig.CreateDefault(); -var server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); +var server = new EmbedIOAuthServer(new Uri("http://localhost:5543/callback"), 5543); server.AuthorizationCodeReceived += async (sender, response) => { await server.Stop(); diff --git a/SpotifyAPI.Docs/docs/authorization_code.md b/SpotifyAPI.Docs/docs/authorization_code.md index 18f3c9d8..d73dc79d 100644 --- a/SpotifyAPI.Docs/docs/authorization_code.md +++ b/SpotifyAPI.Docs/docs/authorization_code.md @@ -10,9 +10,9 @@ title: Authorization Code If you are already in control of a Web-Server (like `ASP.NET`), you can start the flow by generating a login uri: ```csharp -// Make sure "http://localhost:5000" is in your applications redirect URIs! +// Make sure "http://localhost:5543" is in your applications redirect URIs! var loginRequest = new LoginRequest( - new Uri("http://localhost:5000"), + new Uri("http://localhost:5543"), "ClientId", LoginRequest.ResponseType.Code ) @@ -23,14 +23,14 @@ var uri = loginRequest.ToUri(); // Redirect user to uri via your favorite web-server ``` -When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5000` and a `code` parameter is attached to the query. This `code` has to be exchanged for an `access_token` and `refresh_token`: +When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5543` and a `code` parameter is attached to the query. This `code` has to be exchanged for an `access_token` and `refresh_token`: ```csharp -// This method should be called from your web-server when the user visits "http://localhost:5000" +// This method should be called from your web-server when the user visits "http://localhost:5543" public Task GetCallback(string code) { var response = await new OAuthClient().RequestToken( - new AuthorizationCodeTokenRequest("ClientId", "ClientSecret", code, "http://localhost:5000") + new AuthorizationCodeTokenRequest("ClientId", "ClientSecret", code, "http://localhost:5543") ); var spotify = new SpotifyClient(response.AccessToken); @@ -52,7 +52,7 @@ You can also let the `AuthorizationCodeAuthenticator` take care of the refresh p ```csharp var response = await new OAuthClient().RequestToken( - new AuthorizationCodeTokenRequest("ClientId", "ClientSecret", code, "http://localhost:5000") + new AuthorizationCodeTokenRequest("ClientId", "ClientSecret", code, "http://localhost:5543") ); var config = SpotifyClientConfig .CreateDefault() @@ -76,8 +76,8 @@ private static EmbedIOAuthServer _server; public static async Task Main() { - // Make sure "http://localhost:5000/callback" is in your spotify application as redirect uri! - _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); + // Make sure "http://localhost:5543/callback" is in your spotify application as redirect uri! + _server = new EmbedIOAuthServer(new Uri("http://localhost:5543/callback"), 5543); await _server.Start(); _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived; @@ -97,7 +97,7 @@ private static async Task OnAuthorizationCodeReceived(object sender, Authorizati var config = SpotifyClientConfig.CreateDefault(); var tokenResponse = await new OAuthClient(config).RequestToken( new AuthorizationCodeTokenRequest( - "ClientId", "ClientSecret", response.Code, new Uri("http://localhost:5000/callback") + "ClientId", "ClientSecret", response.Code, new Uri("http://localhost:5543/callback") ) ); diff --git a/SpotifyAPI.Docs/docs/example_asp.md b/SpotifyAPI.Docs/docs/example_asp.md index 783ec73b..62ca6138 100644 --- a/SpotifyAPI.Docs/docs/example_asp.md +++ b/SpotifyAPI.Docs/docs/example_asp.md @@ -17,7 +17,7 @@ This example is based on ASP .NET Core. It uses `Authorization Code` under the h ## Run it -Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5001` is a redirect uri of it. +Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5543` is a redirect uri of it. ```bash # Assumes linux and current working directory is the cloned repository @@ -26,5 +26,5 @@ dotnet restore SPOTIFY_CLIENT_ID=YourClientId SPOTIFY_CLIENT_SECRET=YourClientSecret dotnet run -# Visit https://localhost:5001 +# Visit https://localhost:5543 ``` diff --git a/SpotifyAPI.Docs/docs/example_blazor.md b/SpotifyAPI.Docs/docs/example_blazor.md index 13d5550c..8104c035 100644 --- a/SpotifyAPI.Docs/docs/example_blazor.md +++ b/SpotifyAPI.Docs/docs/example_blazor.md @@ -13,7 +13,7 @@ Very similar to the [Blazor WASM Example](example_blazor_wasm.md), but runs code ## Run it -Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5001` is a redirect uri of it. +Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5543` is a redirect uri of it. ```bash # Assumes linux and current working directory is the cloned repository @@ -22,5 +22,5 @@ dotnet restore SPOTIFY_CLIENT_ID=YourClientId SPOTIFY_CLIENT_SECRET=YourClientSecret dotnet run -# Visit https://localhost:5001 +# Visit https://localhost:5543 ``` diff --git a/SpotifyAPI.Docs/docs/example_blazor_wasm.md b/SpotifyAPI.Docs/docs/example_blazor_wasm.md index e30b0508..a0c670dc 100644 --- a/SpotifyAPI.Docs/docs/example_blazor_wasm.md +++ b/SpotifyAPI.Docs/docs/example_blazor_wasm.md @@ -16,7 +16,7 @@ Since this library is compatible with `.NET Standard 2.1`, you can use all featu ## Run it -Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5001` is a redirect uri of it. +Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5543` is a redirect uri of it. ```bash # Assumes linux and current working directory is the cloned repository @@ -26,5 +26,5 @@ dotnet restore echo "{ \"SPOTIFY_CLIENT_ID\": \"YourSpotifyClientId\" }" > wwwroot/appsettings.json dotnet run -# Visit https://localhost:5001 +# Visit https://localhost:5543 ``` diff --git a/SpotifyAPI.Docs/docs/example_cli_custom_html.md b/SpotifyAPI.Docs/docs/example_cli_custom_html.md index 286ec792..acc2935f 100644 --- a/SpotifyAPI.Docs/docs/example_cli_custom_html.md +++ b/SpotifyAPI.Docs/docs/example_cli_custom_html.md @@ -13,7 +13,7 @@ An example to show how you can display your own HTML resource after the user wen ## Run it -Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5001` is a redirect uri of it. +Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5543` is a redirect uri of it. ```bash # Assumes linux and current working directory is the cloned repository diff --git a/SpotifyAPI.Docs/docs/example_cli_persistent_config.md b/SpotifyAPI.Docs/docs/example_cli_persistent_config.md index a70f0ee0..3ecca681 100644 --- a/SpotifyAPI.Docs/docs/example_cli_persistent_config.md +++ b/SpotifyAPI.Docs/docs/example_cli_persistent_config.md @@ -11,7 +11,7 @@ The access and refresh token is saved in a `credentials.json` file of the curren ## Run it -Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5000` is a redirect uri of it. +Before running it, make sure you created an app in your [spotify dashboard](https://developer.spotify.com/dashboard/) and `https://localhost:5543` is a redirect uri of it. ```bash # Assumes linux and current working directory is the cloned repository diff --git a/SpotifyAPI.Docs/docs/implicit_grant.md b/SpotifyAPI.Docs/docs/implicit_grant.md index 94611645..1b83b171 100644 --- a/SpotifyAPI.Docs/docs/implicit_grant.md +++ b/SpotifyAPI.Docs/docs/implicit_grant.md @@ -14,9 +14,9 @@ This flow is useful for getting a user access token for a short timespan. If you are already in control of a Web-Server (like `ASP.NET`), you can start the flow by generating a login uri: ```csharp -// Make sure "http://localhost:5000" is in your applications redirect URIs! +// Make sure "http://localhost:5543" is in your applications redirect URIs! var loginRequest = new LoginRequest( - new Uri("http://localhost:5000"), + new Uri("http://localhost:5543"), "ClientId", LoginRequest.ResponseType.Token ) @@ -27,7 +27,7 @@ var uri = loginRequest.ToUri(); // Redirect user to uri via your favorite web-server ``` -When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5000` and the fragment identifier (`#` part of URI) will contain an access token. +When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5543` and the fragment identifier (`#` part of URI) will contain an access token. :::warning Note, this parameter is not sent to the server! You need JavaScript to access it. @@ -77,15 +77,15 @@ For a real example, have a look at the [Example.UWP](https://github.com/JohnnyCr # Using Spotify.Web.Auth -For cross-platform CLI and desktop apps (non `UWP` apps), custom protocol handlers are sometimes not an option. The fallback here is a small cross-platform embedded web server running on `http://localhost:5000` serving JavaScript. The JavaScript will parse the fragment part of the URI and sends a request to the web server in the background. The web server then notifies your appliciation via an event. +For cross-platform CLI and desktop apps (non `UWP` apps), custom protocol handlers are sometimes not an option. The fallback here is a small cross-platform embedded web server running on `http://localhost:5543` serving JavaScript. The JavaScript will parse the fragment part of the URI and sends a request to the web server in the background. The web server then notifies your appliciation via an event. ```csharp private static EmbedIOAuthServer _server; public static async Task Main() { - // Make sure "http://localhost:5000/callback" is in your spotify application as redirect uri! - _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); + // Make sure "http://localhost:5543/callback" is in your spotify application as redirect uri! + _server = new EmbedIOAuthServer(new Uri("http://localhost:5543/callback"), 5543); await _server.Start(); _server.ImplictGrantReceived += OnImplicitGrantReceived; diff --git a/SpotifyAPI.Docs/docs/pkce.md b/SpotifyAPI.Docs/docs/pkce.md index 7eeaab60..eb6ba5ab 100644 --- a/SpotifyAPI.Docs/docs/pkce.md +++ b/SpotifyAPI.Docs/docs/pkce.md @@ -25,9 +25,9 @@ var (verifier, challenge) = PKCEUtil.GenerateCodes("YourSecureRandomString"); Like most auth flows, you'll need to redirect your user to Spotify's servers so they are able to grant access to your application: ```csharp -// Make sure "http://localhost:5000/callback" is in your applications redirect URIs! +// Make sure "http://localhost:5543/callback" is in your applications redirect URIs! var loginRequest = new LoginRequest( - new Uri("http://localhost:5000/callback"), + new Uri("http://localhost:5543/callback"), "YourClientId", LoginRequest.ResponseType.Code ) @@ -40,15 +40,15 @@ var uri = loginRequest.ToUri(); // Redirect user to uri via your favorite web-server or open a local browser window ``` -When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5000/callback` and a `code` parameter is attached to the query. The redirect URI can also contain a custom protocol paired with UWP App Custom Protocol handler. This received `code` has to be exchanged for an `access_token` and `refresh_token`: +When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to `http://localhost:5543/callback` and a `code` parameter is attached to the query. The redirect URI can also contain a custom protocol paired with UWP App Custom Protocol handler. This received `code` has to be exchanged for an `access_token` and `refresh_token`: ```csharp -// This method should be called from your web-server when the user visits "http://localhost:5000/callback" +// This method should be called from your web-server when the user visits "http://localhost:5543/callback" public Task GetCallback(string code) { // Note that we use the verifier calculated above! var initialResponse = await new OAuthClient().RequestToken( - new PKCETokenRequest("ClientId", code, "http://localhost:5000", verifier) + new PKCETokenRequest("ClientId", code, "http://localhost:5543", verifier) ); var spotify = new SpotifyClient(initialResponse.AccessToken); diff --git a/SpotifyAPI.Web.Examples/Example.CLI.CustomHTML/Program.cs b/SpotifyAPI.Web.Examples/Example.CLI.CustomHTML/Program.cs index 7727e96c..ab271a93 100644 --- a/SpotifyAPI.Web.Examples/Example.CLI.CustomHTML/Program.cs +++ b/SpotifyAPI.Web.Examples/Example.CLI.CustomHTML/Program.cs @@ -24,8 +24,8 @@ namespace Example.CLI.CustomHTML } _server = new EmbedIOAuthServer( - new Uri("http://localhost:5000/callback"), - 5000, + new Uri("http://localhost:5543/callback"), + 5543, Assembly.GetExecutingAssembly(), "Example.CLI.CustomHTML.Resources.custom_site" ); @@ -38,7 +38,7 @@ namespace Example.CLI.CustomHTML Scope = new List { UserReadEmail } }; - Uri uri = request.ToUri(); + var uri = request.ToUri(); try { BrowserUtil.Open(uri); @@ -48,14 +48,14 @@ namespace Example.CLI.CustomHTML Console.WriteLine("Unable to open URL, manually open: {0}", uri); } - Console.ReadKey(); + _ = Console.ReadKey(); } private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response) { await _server.Stop(); - AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken( + var token = await new OAuthClient().RequestToken( new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.BaseUri) ); diff --git a/SpotifyAPI.Web.Examples/Example.CLI.PersistentConfig/Program.cs b/SpotifyAPI.Web.Examples/Example.CLI.PersistentConfig/Program.cs index eb230cdd..62ce0f9d 100644 --- a/SpotifyAPI.Web.Examples/Example.CLI.PersistentConfig/Program.cs +++ b/SpotifyAPI.Web.Examples/Example.CLI.PersistentConfig/Program.cs @@ -5,22 +5,25 @@ using System.Threading.Tasks; using Newtonsoft.Json; using SpotifyAPI.Web; using SpotifyAPI.Web.Auth; -using Swan.Logging; using static SpotifyAPI.Web.Scopes; namespace Example.CLI.PersistentConfig { /// /// This is a basic example how to get user access using the Auth package and a CLI Program - /// Your spotify app needs to have http://localhost:5000 as redirect uri whitelisted + /// Your spotify app needs to have http://localhost:5543 as redirect uri whitelisted /// public class Program { private const string CredentialsPath = "credentials.json"; private static readonly string? clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID"); - private static readonly EmbedIOAuthServer _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); + private static readonly EmbedIOAuthServer _server = new(new Uri("http://localhost:5543/callback"), 5543); + + private static void Exiting() + { + Console.CursorVisible = true; + } - private static void Exiting() => Console.CursorVisible = true; public static async Task Main() { // This is a bug in the SWAN Logging library, need this hack to bring back the cursor @@ -42,7 +45,7 @@ namespace Example.CLI.PersistentConfig await StartAuthentication(); } - Console.ReadKey(); + _ = Console.ReadKey(); return 0; } @@ -77,7 +80,7 @@ namespace Example.CLI.PersistentConfig _server.AuthorizationCodeReceived += async (sender, response) => { await _server.Stop(); - PKCETokenResponse token = await new OAuthClient().RequestToken( + var token = await new OAuthClient().RequestToken( new PKCETokenRequest(clientId!, response.Code, _server.BaseUri, verifier) ); @@ -92,7 +95,7 @@ namespace Example.CLI.PersistentConfig Scope = new List { UserReadEmail, UserReadPrivate, PlaylistReadPrivate, PlaylistReadCollaborative } }; - Uri uri = request.ToUri(); + var uri = request.ToUri(); try { BrowserUtil.Open(uri); diff --git a/SpotifyAPI.Web.Examples/Example.TokenSwap/Client/Program.cs b/SpotifyAPI.Web.Examples/Example.TokenSwap/Client/Program.cs index 4b28910f..f8b639d0 100644 --- a/SpotifyAPI.Web.Examples/Example.TokenSwap/Client/Program.cs +++ b/SpotifyAPI.Web.Examples/Example.TokenSwap/Client/Program.cs @@ -13,7 +13,7 @@ namespace Client public static async Task Main() { - _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); + _server = new EmbedIOAuthServer(new Uri("http://localhost:5543/callback"), 5543); await _server.Start(); _server.AuthorizationCodeReceived += OnAuthorizationCodeReceived; @@ -23,7 +23,7 @@ namespace Client Scope = new List { Scopes.UserReadEmail } }; - Uri uri = request.ToUri(); + var uri = request.ToUri(); try { BrowserUtil.Open(uri); @@ -33,20 +33,20 @@ namespace Client Console.WriteLine("Unable to open URL, manually open: {0}", uri); } - Console.ReadKey(); + _ = Console.ReadKey(); } private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response) { var oauth = new OAuthClient(); - var tokenRequest = new TokenSwapTokenRequest(new Uri("http://localhost:5001/swap"), response.Code); + var tokenRequest = new TokenSwapTokenRequest(new Uri("http://localhost:5543/swap"), response.Code); var tokenResponse = await oauth.RequestToken(tokenRequest); Console.WriteLine($"We got an access token from server: {tokenResponse.AccessToken}"); var refreshRequest = new TokenSwapRefreshRequest( - new Uri("http://localhost:5001/refresh"), + new Uri("http://localhost:5543/refresh"), tokenResponse.RefreshToken ); var refreshResponse = await oauth.RequestToken(refreshRequest); diff --git a/SpotifyAPI.Web.Examples/Example.TokenSwap/Server/index.js b/SpotifyAPI.Web.Examples/Example.TokenSwap/Server/index.js index bba97378..19178f52 100644 --- a/SpotifyAPI.Web.Examples/Example.TokenSwap/Server/index.js +++ b/SpotifyAPI.Web.Examples/Example.TokenSwap/Server/index.js @@ -1,44 +1,52 @@ -const express = require('express'); -const bodyParser = require('body-parser'); -const axios = require('axios'); -const { default: Axios } = require('axios'); +const express = require("express"); +const bodyParser = require("body-parser"); +const axios = require("axios"); +const { default: Axios } = require("axios"); -const PORT = process.env.PORT || '5001'; +const PORT = process.env.PORT || "5543"; const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET; const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_ID; if (!SPOTIFY_CLIENT_SECRET || !SPOTIFY_CLIENT_ID) { - console.log("SPOTIFY_CLIENT_SECRET or SPOTIFY_CLIENT_ID environment variable is not set!"); + console.log( + "SPOTIFY_CLIENT_SECRET or SPOTIFY_CLIENT_ID environment variable is not set!" + ); process.exit(1); } const app = express(); app.use(bodyParser.urlencoded({ extended: true })); -app.post('/swap', async (req, res) => { +app.post("/swap", async (req, res) => { const { code } = req.body; const params = new URLSearchParams(); - params.append('grant_type', 'authorization_code'); - params.append('code', code); - params.append('redirect_uri', 'http://localhost:5000/callback'); - params.append('client_secret', SPOTIFY_CLIENT_SECRET); - params.append('client_id', SPOTIFY_CLIENT_ID); + params.append("grant_type", "authorization_code"); + params.append("code", code); + params.append("redirect_uri", "http://localhost:5543/callback"); + params.append("client_secret", SPOTIFY_CLIENT_SECRET); + params.append("client_id", SPOTIFY_CLIENT_ID); - const { data } = await Axios.post('https://accounts.spotify.com/api/token', params); + const { data } = await Axios.post( + "https://accounts.spotify.com/api/token", + params + ); return res.send(data); }); -app.post('/refresh', async (req, res) => { +app.post("/refresh", async (req, res) => { const { refresh_token } = req.body; const params = new URLSearchParams(); - params.append('grant_type', 'refresh_token'); - params.append('refresh_token', refresh_token); - params.append('client_secret', SPOTIFY_CLIENT_SECRET); - params.append('client_id', SPOTIFY_CLIENT_ID); + params.append("grant_type", "refresh_token"); + params.append("refresh_token", refresh_token); + params.append("client_secret", SPOTIFY_CLIENT_SECRET); + params.append("client_id", SPOTIFY_CLIENT_ID); - const { data } = await Axios.post('https://accounts.spotify.com/api/token', params); + const { data } = await Axios.post( + "https://accounts.spotify.com/api/token", + params + ); return res.send(data); });