mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 06:36:26 +00:00
Replace port 5000 (often used) with 5543, fixes #868
This commit is contained in:
parent
dfcd8a182b
commit
ae55ddb39b
@ -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();
|
||||
|
@ -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")
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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<string> { 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)
|
||||
);
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
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<int> 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<string> { UserReadEmail, UserReadPrivate, PlaylistReadPrivate, PlaylistReadCollaborative }
|
||||
};
|
||||
|
||||
Uri uri = request.ToUri();
|
||||
var uri = request.ToUri();
|
||||
try
|
||||
{
|
||||
BrowserUtil.Open(uri);
|
||||
|
@ -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<string> { 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);
|
||||
|
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user