public static async void DoAuthAsync() { if (Settings.Settings.UseOwnApp) { _auth = new TokenSwapAuth( exchangeServerUri: "https://songify.rocks/auth/auth.php?id=" + Settings.Settings.ClientId + "&secret=" + Settings.Settings.ClientSecret, serverUri: "http://*****:*****@"Own ID"); } else { _auth = new TokenSwapAuth( exchangeServerUri: "https://songify.rocks/auth/_index.php", serverUri: "http://*****:*****@"Songify ID"); } try { // Execute the authentication flow and subscribe the timer elapsed event AuthRefresh.Elapsed += AuthRefresh_Elapsed; // If Refresh and Access-token are present, just refresh the auth if (!string.IsNullOrEmpty(Settings.Settings.RefreshToken) && !string.IsNullOrEmpty(Settings.Settings.AccessToken)) { Authed = true; Spotify = new SpotifyWebAPI() { TokenType = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).TokenType, AccessToken = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken }; Spotify.AccessToken = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken; } else { Authed = false; } // if the auth was successful save the new tokens and _auth.AuthReceived += async(sender, response) => { if (Authed) { return; } LastToken = await _auth.ExchangeCodeAsync(response.Code); // Save tokens Settings.Settings.RefreshToken = LastToken.RefreshToken; Settings.Settings.AccessToken = LastToken.AccessToken; // create ne Spotify object Spotify = new SpotifyWebAPI() { TokenType = LastToken.TokenType, AccessToken = LastToken.AccessToken }; Authenticated = true; _auth.Stop(); Authed = true; AuthRefresh.Start(); await Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { foreach (Window window in Application.Current.Windows) { if (window.GetType() != typeof(Window_Settings)) { continue; } ((Window_Settings)window).SetControls(); } })); }; // automatically refreshes the token after it expires _auth.OnAccessTokenExpired += async(sender, e) => { Spotify.AccessToken = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken; Settings.Settings.RefreshToken = LastToken.RefreshToken; Settings.Settings.AccessToken = Spotify.AccessToken; }; _auth.Start(); if (Authed) { AuthRefresh.Start(); return; } _auth.OpenBrowser(); } catch (Exception ex) { Logger.LogExc(ex); } }
public static async void DoAuthAsync() { try { // Execute the authentication flow and subscribe the timer elapsed event authRefresh.Elapsed += AuthRefresh_Elapsed; // If Refresh and Accesstoken are present, just refresh the auth if (!string.IsNullOrEmpty(Settings.RefreshToken) && !string.IsNullOrEmpty(Settings.AccessToken)) { authed = true; spotify = new SpotifyWebAPI() { TokenType = (await auth.RefreshAuthAsync(Settings.RefreshToken)).TokenType, AccessToken = (await auth.RefreshAuthAsync(Settings.RefreshToken)).AccessToken }; spotify.AccessToken = (await auth.RefreshAuthAsync(Settings.RefreshToken)).AccessToken; } else { authed = false; } // if the auth was successfull save the new tokens and auth.AuthReceived += async(sender, response) => { if (authed) { return; } lastToken = await auth.ExchangeCodeAsync(response.Code); // Save tokens Settings.RefreshToken = lastToken.RefreshToken; Settings.AccessToken = lastToken.AccessToken; // create ne Spotify object spotify = new SpotifyWebAPI() { TokenType = lastToken.TokenType, AccessToken = lastToken.AccessToken }; authenticated = true; auth.Stop(); authRefresh.Start(); await Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { foreach (Window window in System.Windows.Application.Current.Windows) { if (window.GetType() != typeof(SettingsWindow)) { continue; } ((SettingsWindow)window).SetControls(); } })); }; // autmatically refreshes the token after it expires auth.OnAccessTokenExpired += async(sender, e) => { spotify.AccessToken = (await auth.RefreshAuthAsync(Settings.RefreshToken)).AccessToken; Settings.RefreshToken = lastToken.RefreshToken; Settings.AccessToken = spotify.AccessToken; }; auth.Start(); if (authed) { authRefresh.Start(); return; } auth.OpenBrowser(); } catch (Exception ex) { Logger.LogExc(ex); } }
public async Task <SpotifyWebAPI> GetClientAsync() { _timer.Elapsed += OnAutoRefresh; var cfg = JsonConfig.Load(); // If Refresh and Accesstoken are present, just refresh the auth if (!string.IsNullOrEmpty(cfg.RefreshToken) && !string.IsNullOrEmpty(cfg.AccessToken)) { _isAuthenticated = true; var rt = await _auth.RefreshAuthAsync(cfg.RefreshToken); if (rt == null) { throw new NullReferenceException(nameof(rt)); } _spotify = new SpotifyWebAPI() { TokenType = rt.TokenType, AccessToken = rt.AccessToken }; cfg.AccessToken = rt.AccessToken; JsonConfig.Save(cfg); } else { _isAuthenticated = false; } _auth.AuthReceived += async(sender, payload) => { if (_isAuthenticated) { return; } _lastToken = await _auth.ExchangeCodeAsync(payload.Code); cfg.RefreshToken = _lastToken.RefreshToken; cfg.AccessToken = _lastToken.AccessToken; JsonConfig.Save(cfg); _spotify = new SpotifyWebAPI() { TokenType = _lastToken.TokenType, AccessToken = _lastToken.AccessToken, }; _auth.Stop(); }; _auth.OnAccessTokenExpired += async(sender, payload) => { _spotify.AccessToken = (await _auth.RefreshAuthAsync(cfg.RefreshToken)).AccessToken; cfg.RefreshToken = _lastToken.RefreshToken; cfg.AccessToken = _spotify.AccessToken; JsonConfig.Save(cfg); }; _auth.Start(); if (_isAuthenticated) { _timer.Start(); } else { _auth.OpenBrowser(); } for (int i = 0; i < _timeoutSeconds * 4; i++) { if (_spotify != null) { return(_spotify); } else { await Task.Delay(250); } } // We've reached the time out for the creatoion of the client throw new TimeoutException("The creation of an spotify client has timed out."); }