2020-05-16 21:31:44 +01:00
|
|
|
using System.Diagnostics;
|
2020-05-15 19:06:24 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System;
|
|
|
|
using SpotifyAPI.Web.Auth;
|
|
|
|
using SpotifyAPI.Web.Http;
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Newtonsoft.Json;
|
2020-05-15 13:05:27 +01:00
|
|
|
using static SpotifyAPI.Web.Scopes;
|
2020-05-15 19:06:24 +01:00
|
|
|
|
2020-05-16 21:31:44 +01:00
|
|
|
namespace Example.CLI.PersistentConfig
|
2020-05-15 19:06:24 +01:00
|
|
|
{
|
|
|
|
/// <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
|
|
|
|
/// </summary>
|
|
|
|
public class Program
|
|
|
|
{
|
|
|
|
private const string CredentialsPath = "credentials.json";
|
|
|
|
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<int> Main()
|
|
|
|
{
|
|
|
|
if (File.Exists(CredentialsPath))
|
|
|
|
{
|
|
|
|
await Start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await StartAuthentication();
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task Start()
|
|
|
|
{
|
|
|
|
var json = await File.ReadAllTextAsync(CredentialsPath);
|
|
|
|
var token = JsonConvert.DeserializeObject<AuthorizationCodeTokenResponse>(json);
|
|
|
|
|
|
|
|
var authenticator = new AuthorizationCodeAuthenticator(clientId, clientSecret, token);
|
|
|
|
authenticator.TokenRefreshed += (sender, token) => File.WriteAllText(CredentialsPath, JsonConvert.SerializeObject(token));
|
|
|
|
|
|
|
|
var config = SpotifyClientConfig.CreateDefault()
|
|
|
|
.WithAuthenticator(authenticator);
|
|
|
|
|
|
|
|
var spotify = new SpotifyClient(config);
|
|
|
|
|
|
|
|
var me = await spotify.UserProfile.Current();
|
|
|
|
Console.WriteLine($"Welcome {me.DisplayName} ({me.Id}), your authenticated!");
|
|
|
|
|
|
|
|
var playlists = await spotify.Paginate(await spotify.Playlists.CurrentUsers());
|
|
|
|
Console.WriteLine($"Total Playlists in your Account: {playlists.Count}");
|
|
|
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task StartAuthentication()
|
|
|
|
{
|
2020-05-15 21:02:54 +01:00
|
|
|
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
|
2020-05-15 19:06:24 +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 19:06:24 +01:00
|
|
|
{
|
2020-05-15 13:05:27 +01:00
|
|
|
Scope = new List<string> { UserReadEmail, UserReadPrivate, PlaylistReadPrivate }
|
2020-05-15 19:06:24 +01:00
|
|
|
};
|
|
|
|
|
2020-05-18 10:39:01 +01:00
|
|
|
Uri uri = request.ToUri();
|
2020-05-15 19:06:24 +01:00
|
|
|
try
|
|
|
|
{
|
2020-05-18 10:39:01 +01:00
|
|
|
BrowserUtil.Open(uri);
|
2020-05-15 19:06:24 +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 19:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task OnAuthorizationCodeReceived(object sender, AuthorizationCodeResponse response)
|
|
|
|
{
|
|
|
|
await _server.Stop();
|
|
|
|
AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken(
|
2020-05-15 21:02:54 +01:00
|
|
|
new AuthorizationCodeTokenRequest(clientId, clientSecret, response.Code, _server.BaseUri)
|
2020-05-15 19:06:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await File.WriteAllTextAsync(CredentialsPath, JsonConvert.SerializeObject(token));
|
|
|
|
await Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|