2020-05-15 20:41:53 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-11-27 12:29:35 +00:00
|
|
|
using System.Reflection;
|
2020-05-15 20:41:53 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
using SpotifyAPI.Web.Auth;
|
|
|
|
using static SpotifyAPI.Web.Scopes;
|
|
|
|
|
2020-05-16 21:31:44 +01:00
|
|
|
namespace Example.CLI.CustomHTML
|
2020-05-15 20:41:53 +01:00
|
|
|
{
|
|
|
|
public class Program
|
|
|
|
{
|
|
|
|
private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
|
|
|
|
private static readonly string clientSecret = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_SECRET");
|
|
|
|
private static EmbedIOAuthServer _server;
|
|
|
|
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
2022-05-21 19:13:30 +01:00
|
|
|
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
|
|
|
|
{
|
|
|
|
throw new NullReferenceException(
|
|
|
|
"Please set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET via environment variables before starting the program"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:41:53 +01:00
|
|
|
_server = new EmbedIOAuthServer(
|
2023-05-27 20:42:31 +01:00
|
|
|
new Uri("http://localhost:5543/callback"),
|
|
|
|
5543,
|
2020-05-15 21:02:54 +01:00
|
|
|
Assembly.GetExecutingAssembly(),
|
2020-05-16 21:42:18 +01:00
|
|
|
"Example.CLI.CustomHTML.Resources.custom_site"
|
2020-05-15 21:02:54 +01:00
|
|
|
);
|
2020-05-15 20:41:53 +01:00
|
|
|
await _server.Start();
|
|
|
|
|
|
|
|
_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;
|
|
|
|
|
2020-05-18 10:39:01 +01:00
|
|
|
var request = new LoginRequest(_server.BaseUri, clientId, LoginRequest.ResponseType.Code)
|
2020-05-15 20:41:53 +01:00
|
|
|
{
|
|
|
|
Scope = new List<string> { UserReadEmail }
|
|
|
|
};
|
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
var uri = request.ToUri();
|
2020-05-15 20:41:53 +01:00
|
|
|
try
|
|
|
|
{
|
2020-05-18 10:39:01 +01:00
|
|
|
BrowserUtil.Open(uri);
|
2020-05-15 20:41:53 +01:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2020-05-18 10:39:01 +01:00
|
|
|
Console.WriteLine("Unable to open URL, manually open: {0}", uri);
|
2020-05-15 20:41:53 +01:00
|
|
|
}
|
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
_ = Console.ReadKey();
|
2020-05-15 20:41:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
|
|
|
|
{
|
|
|
|
await _server.Stop();
|
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
var token = await new OAuthClient().RequestToken(
|
2020-05-15 21:02:54 +01:00
|
|
|
new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.BaseUri)
|
2020-05-15 20:41:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
var config = SpotifyClientConfig.CreateDefault().WithToken(token.AccessToken, token.TokenType);
|
|
|
|
var spotify = new SpotifyClient(config);
|
|
|
|
|
|
|
|
var me = await spotify.UserProfile.Current();
|
|
|
|
|
|
|
|
Console.WriteLine($"Your E-Mail: {me.Email}");
|
2020-05-15 21:02:54 +01:00
|
|
|
Environment.Exit(0);
|
2020-05-15 20:41:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|