Skip to content

Commit ef60a9f

Browse files
committed
Added TokenSwapAuth and console example for it JohnnyCrazy#451
1 parent 7801f84 commit ef60a9f

10 files changed

Lines changed: 632 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
</Project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using SpotifyAPI.Web;
5+
using SpotifyAPI.Web.Auth;
6+
7+
namespace Client
8+
{
9+
public class Program
10+
{
11+
private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
12+
private static EmbedIOAuthServer _server;
13+
14+
public static async Task Main(string[] args)
15+
{
16+
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
17+
await _server.Start();
18+
19+
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
20+
21+
var request = new LoginRequest(_server.BaseUri, clientId, LoginRequest.ResponseType.Code)
22+
{
23+
Scope = new List<string> { Scopes.UserReadEmail }
24+
};
25+
26+
Uri uri = request.ToUri();
27+
try
28+
{
29+
BrowserUtil.Open(uri);
30+
}
31+
catch (Exception)
32+
{
33+
Console.WriteLine("Unable to open URL, manually open: {0}", uri);
34+
}
35+
36+
Console.ReadKey();
37+
}
38+
39+
private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
40+
{
41+
var oauth = new OAuthClient();
42+
43+
var tokenRequest = new TokenSwapTokenRequest(new Uri("http://localhost:5001/swap"), response.Code);
44+
var tokenResponse = await oauth.RequestToken(tokenRequest);
45+
46+
Console.WriteLine($"We got an access token from server: {tokenResponse.AccessToken}");
47+
48+
var refreshRequest = new TokenSwapRefreshRequest(
49+
new Uri("http://localhost:5001/refresh"),
50+
tokenResponse.RefreshToken
51+
);
52+
var refreshResponse = await oauth.RequestToken(refreshRequest);
53+
54+
Console.WriteLine($"We got a new refreshed access token from server: {refreshResponse.AccessToken}");
55+
}
56+
}
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const axios = require('axios');
4+
const { default: Axios } = require('axios');
5+
6+
const PORT = process.env.PORT || '5001';
7+
const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET;
8+
const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_ID;
9+
if (!SPOTIFY_CLIENT_SECRET || !SPOTIFY_CLIENT_ID) {
10+
console.log("SPOTIFY_CLIENT_SECRET or SPOTIFY_CLIENT_ID environment variable is not set!");
11+
process.exit(1);
12+
}
13+
14+
const app = express();
15+
app.use(bodyParser.urlencoded({ extended: true }));
16+
17+
app.post('/swap', async (req, res) => {
18+
const { code } = req.body;
19+
20+
const params = new URLSearchParams();
21+
params.append('grant_type', 'authorization_code');
22+
params.append('code', code);
23+
params.append('redirect_uri', 'http://localhost:5000/callback');
24+
params.append('client_secret', SPOTIFY_CLIENT_SECRET);
25+
params.append('client_id', SPOTIFY_CLIENT_ID);
26+
27+
const { data } = await Axios.post('https://accounts.spotify.com/api/token', params);
28+
29+
return res.send(data);
30+
});
31+
32+
app.post('/refresh', async (req, res) => {
33+
const { refresh_token } = req.body;
34+
35+
const params = new URLSearchParams();
36+
params.append('grant_type', 'refresh_token');
37+
params.append('refresh_token', refresh_token);
38+
params.append('client_secret', SPOTIFY_CLIENT_SECRET);
39+
params.append('client_id', SPOTIFY_CLIENT_ID);
40+
41+
const { data } = await Axios.post('https://accounts.spotify.com/api/token', params);
42+
43+
return res.send(data);
44+
});
45+
46+
app.listen(PORT, () => {
47+
console.log(`Server listening on ${PORT} 🚀`);
48+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Server",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"dependencies": {
10+
"axios": "^0.19.2",
11+
"body-parser": "^1.19.0",
12+
"express": "^4.17.1"
13+
}
14+
}

0 commit comments

Comments
 (0)