Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (46 loc) · 1.69 KB
/
Copy pathProgram.cs
File metadata and controls
57 lines (46 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
namespace Client
{
public class Program
{
private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
private static EmbedIOAuthServer _server;
public static async Task Main()
{
_server = new EmbedIOAuthServer(new Uri("http://localhost:5543/callback"), 5543);
await _server.Start();
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
var request = new LoginRequest(_server.BaseUri, clientId, LoginRequest.ResponseType.Code)
{
Scope = new List<string> { Scopes.UserReadEmail }
};
var uri = request.ToUri();
try
{
BrowserUtil.Open(uri);
}
catch (Exception)
{
Console.WriteLine("Unable to open URL, manually open: {0}", uri);
}
_ = Console.ReadKey();
}
private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
{
var oauth = new OAuthClient();
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:5543/refresh"),
tokenResponse.RefreshToken
);
var refreshResponse = await oauth.RequestToken(refreshRequest);
Console.WriteLine($"We got a new refreshed access token from server: {refreshResponse.AccessToken}");
}
}
}