2018-08-24 13:10:13 +01:00
|
|
|
|
using System;
|
2019-11-12 14:02:30 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-08-24 13:10:13 +01:00
|
|
|
|
using SpotifyAPI.Web.Auth;
|
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
|
|
2019-11-12 14:02:30 +00:00
|
|
|
|
namespace SpotifyAPI.Web.Examples.CLI
|
2018-08-24 13:10:13 +01:00
|
|
|
|
{
|
2018-09-21 13:45:14 +01:00
|
|
|
|
internal static class Program
|
2018-08-24 13:10:13 +01:00
|
|
|
|
{
|
|
|
|
|
private static string _clientId = ""; //"";
|
|
|
|
|
private static string _secretId = ""; //"";
|
|
|
|
|
|
2019-03-18 20:24:09 +00:00
|
|
|
|
// ReSharper disable once UnusedParameter.Local
|
2019-11-12 14:02:30 +00:00
|
|
|
|
public static void Main(string[] args)
|
2018-08-24 13:10:13 +01:00
|
|
|
|
{
|
|
|
|
|
_clientId = string.IsNullOrEmpty(_clientId)
|
2018-09-21 13:45:14 +01:00
|
|
|
|
? Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID")
|
2018-08-24 13:10:13 +01:00
|
|
|
|
: _clientId;
|
|
|
|
|
|
|
|
|
|
_secretId = string.IsNullOrEmpty(_secretId)
|
2018-09-21 13:45:14 +01:00
|
|
|
|
? Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID")
|
2018-08-24 13:10:13 +01:00
|
|
|
|
: _secretId;
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("####### Spotify API Example #######");
|
|
|
|
|
Console.WriteLine("This example uses AuthorizationCodeAuth.");
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
|
2018-12-22 20:15:29 +00:00
|
|
|
|
|
2019-11-12 14:02:30 +00:00
|
|
|
|
var auth =
|
2018-12-22 20:15:29 +00:00
|
|
|
|
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
|
|
|
|
|
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
|
2018-12-22 20:39:46 +00:00
|
|
|
|
auth.AuthReceived += AuthOnAuthReceived;
|
2018-12-22 20:15:29 +00:00
|
|
|
|
auth.Start();
|
|
|
|
|
auth.OpenBrowser();
|
|
|
|
|
|
2018-08-24 13:10:13 +01:00
|
|
|
|
Console.ReadLine();
|
2018-09-04 13:39:07 +01:00
|
|
|
|
auth.Stop(0);
|
2019-08-16 23:40:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-08-24 13:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)
|
|
|
|
|
{
|
2019-11-12 14:02:30 +00:00
|
|
|
|
var auth = (AuthorizationCodeAuth) sender;
|
2018-08-24 13:10:13 +01:00
|
|
|
|
auth.Stop();
|
|
|
|
|
|
|
|
|
|
Token token = await auth.ExchangeCode(payload.Code);
|
2019-11-12 14:02:30 +00:00
|
|
|
|
var api = new SpotifyWebAPI
|
2018-08-24 13:10:13 +01:00
|
|
|
|
{
|
|
|
|
|
AccessToken = token.AccessToken,
|
|
|
|
|
TokenType = token.TokenType
|
|
|
|
|
};
|
2019-11-12 14:02:30 +00:00
|
|
|
|
await PrintUsefulData(api);
|
2018-08-24 13:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 14:02:30 +00:00
|
|
|
|
private static async Task PrintAllPlaylistTracks(SpotifyWebAPI api, Paging<SimplePlaylist> playlists)
|
|
|
|
|
{
|
|
|
|
|
if (playlists.Items == null) return;
|
|
|
|
|
|
|
|
|
|
playlists.Items.ForEach(playlist => Console.WriteLine($"- {playlist.Name}"));
|
|
|
|
|
if(playlists.HasNextPage())
|
|
|
|
|
await PrintAllPlaylistTracks(api, await api.GetNextPageAsync(playlists));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task PrintUsefulData(SpotifyWebAPI api)
|
2019-08-16 23:40:04 +01:00
|
|
|
|
{
|
2018-08-24 13:10:13 +01:00
|
|
|
|
PrivateProfile profile = await api.GetPrivateProfileAsync();
|
|
|
|
|
string name = string.IsNullOrEmpty(profile.DisplayName) ? profile.Id : profile.DisplayName;
|
|
|
|
|
Console.WriteLine($"Hello there, {name}!");
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Your playlists:");
|
2019-11-12 14:02:30 +00:00
|
|
|
|
await PrintAllPlaylistTracks(api, api.GetUserPlaylists(profile.Id));
|
2018-08-24 13:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|