1- using System . Diagnostics ;
21using System . IO ;
32using System . Threading . Tasks ;
43using System ;
@@ -18,12 +17,19 @@ namespace Example.CLI.PersistentConfig
1817 public class Program
1918 {
2019 private const string CredentialsPath = "credentials.json" ;
21- private static readonly string clientId = Environment . GetEnvironmentVariable ( "SPOTIFY_CLIENT_ID" ) ;
22- private static readonly string clientSecret = Environment . GetEnvironmentVariable ( "SPOTIFY_CLIENT_SECRET" ) ;
23- private static EmbedIOAuthServer _server ;
20+ private static readonly string ? clientId = Environment . GetEnvironmentVariable ( "SPOTIFY_CLIENT_ID" ) ;
21+ private static readonly string ? clientSecret = Environment . GetEnvironmentVariable ( "SPOTIFY_CLIENT_SECRET" ) ;
22+ private static readonly EmbedIOAuthServer _server = new EmbedIOAuthServer ( new Uri ( "http://localhost:5000/callback" ) , 5000 ) ;
2423
2524 public static async Task < int > Main ( )
2625 {
26+ if ( string . IsNullOrEmpty ( clientId ) || string . IsNullOrEmpty ( clientSecret ) )
27+ {
28+ throw new NullReferenceException (
29+ "Please set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET via environment variables before starting the program"
30+ ) ;
31+ }
32+
2733 if ( File . Exists ( CredentialsPath ) )
2834 {
2935 await Start ( ) ;
@@ -42,7 +48,7 @@ private static async Task Start()
4248 var json = await File . ReadAllTextAsync ( CredentialsPath ) ;
4349 var token = JsonConvert . DeserializeObject < AuthorizationCodeTokenResponse > ( json ) ;
4450
45- var authenticator = new AuthorizationCodeAuthenticator ( clientId , clientSecret , token ) ;
51+ var authenticator = new AuthorizationCodeAuthenticator ( clientId ! , clientSecret ! , token ) ;
4652 authenticator . TokenRefreshed += ( sender , token ) => File . WriteAllText ( CredentialsPath , JsonConvert . SerializeObject ( token ) ) ;
4753
4854 var config = SpotifyClientConfig . CreateDefault ( )
@@ -61,11 +67,10 @@ private static async Task Start()
6167
6268 private static async Task StartAuthentication ( )
6369 {
64- _server = new EmbedIOAuthServer ( new Uri ( "http://localhost:5000/callback" ) , 5000 ) ;
6570 await _server . Start ( ) ;
6671 _server . AuthorizationCodeReceived += OnAuthorizationCodeReceived ;
6772
68- var request = new LoginRequest ( _server . BaseUri , clientId , LoginRequest . ResponseType . Code )
73+ var request = new LoginRequest ( _server . BaseUri , clientId ! , LoginRequest . ResponseType . Code )
6974 {
7075 Scope = new List < string > { UserReadEmail , UserReadPrivate , PlaylistReadPrivate }
7176 } ;
@@ -85,7 +90,7 @@ private static async Task OnAuthorizationCodeReceived(object sender, Authorizati
8590 {
8691 await _server . Stop ( ) ;
8792 AuthorizationCodeTokenResponse token = await new OAuthClient ( ) . RequestToken (
88- new AuthorizationCodeTokenRequest ( clientId , clientSecret , response . Code , _server . BaseUri )
93+ new AuthorizationCodeTokenRequest ( clientId ! , clientSecret ! , response . Code , _server . BaseUri )
8994 ) ;
9095
9196 await File . WriteAllTextAsync ( CredentialsPath , JsonConvert . SerializeObject ( token ) ) ;
0 commit comments