private static SpotifyWebAPI GetWebClient(SpotifyUser oSpotifyUser) { AutorizationCodeAuth oAuth = new AutorizationCodeAuth() { ClientId = _ClientPublic, RedirectUri = _RedirectUrl, Scope = Scope.UserReadPrivate | Scope.UserReadPrivate | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserFollowRead }; //oAuth.StartHttpServer();//Not sure if this is needed or not but what the hell why not! if (oSpotifyUser.AccessToken.AccessExpired)//The user has authorized us and was tokenized but the temp access token has expired { Token oToken = oAuth.RefreshToken(oSpotifyUser.RefreshToken.Code, _ClientPrivate); if (oToken.Error == null) { oSpotifyUser.UpdateUserWithToken(oToken); } } //oAuth.StopHttpServer(); SpotifyWebAPI oWebClient = new SpotifyWebAPI() { AccessToken = oSpotifyUser.AccessToken.Code, TokenType = oSpotifyUser.AccessToken.TokenType, UseAuth = true }; return(oWebClient); }
public CustomToken RefreshToken(string refreshToken, string clientSecret) { AutorizationCodeAuth auth = new AutorizationCodeAuth() { ClientId = Constants.ClientId, State = Constants.StateKey }; Token response; CustomToken result = null; try { response = auth.RefreshToken(refreshToken, clientSecret); result = response.ToCustomToken(); if (result != null) { result.RefreshToken = refreshToken; } } catch (Exception ex) { } return(result); }
private void RefreshToken() { lock (auth) { var token = auth.RefreshToken(config.Comms.AuthToken.RefreshToken, config.Comms.ClientSecret); if (string.IsNullOrWhiteSpace(token.Error)) { if (string.IsNullOrWhiteSpace(token.RefreshToken)) { // Refresh token may not always be replaced. token.RefreshToken = config.Comms.AuthToken.RefreshToken; } config.Comms.AuthToken = token; config.WriteComms(); IsAuthorised = true; } else { MessageBox.Show($"Error refreshing token: {token.Error}\n\n{token.ErrorDescription}", "Auth Error"); IsAuthorised = false; } } // ReSharper disable once PossibleLossOfFraction tokenRefreshTimer.Change( TimeSpan.FromSeconds(config.Comms.AuthToken.ExpiresIn / 2), Timeout.InfiniteTimeSpan); }
private static void _refreshToken() { _token = Auth.RefreshToken(Config.GetDefault("spotify/auth/refreshToken", "", "credentials"), Config.GetDefault("spotify/auth/clientSecret", "", "credentials")); if (Spotify != null) { Spotify.AccessToken = _token.AccessToken; } }
public RockolappScreenData GetCurrentTrack(string spotifyUserId) { string token; var user = dataAccess.GetUser(spotifyUserId); var spotify = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = user.Token, UseAuth = true }; PlaybackContext context = spotify.GetPlayingTrack(); if (context.HasError() && context.Error.Status == 401) { AutorizationCodeAuth auth = new AutorizationCodeAuth(); //Datos de mi aplicacion Rocolapp auth.ClientId = DataAccess.GetRocolappData().ClientId; auth.RedirectUri = DataAccess.GetRocolappData().RedirectUri; string clientSecret = DataAccess.GetRocolappData().ClientSecret; Token newToken = auth.RefreshToken(user.RefreshToken, DataAccess.GetRocolappData().ClientSecret); dataAccess.UpdateToken(user.Id.ToString(), newToken.AccessToken); token = newToken.AccessToken; var withRefreshedToken = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = newToken.AccessToken, UseAuth = true }; context = withRefreshedToken.GetPlayingTrack(); } RockolappScreenData screenData = new RockolappScreenData(); try { screenData.TrackName = context.Item.Name; screenData.Artist = context.Item.Artists.First().Name; screenData.Duration = context.Item.DurationMs; screenData.LapsedTime = context.ProgressMs; screenData.ImageUrl = context.Item.Album.Images.First().Url; } catch (Exception) { } return(screenData); }
private Token RefreshToken(string refreshToken, string clientSecret) { AutorizationCodeAuth auth = new AutorizationCodeAuth() { ClientId = Constants.ClientId, State = Constants.StateKey }; return(auth.RefreshToken(refreshToken, clientSecret)); }
public static Token CreateNewToken(string refreshToken, string userId) { AutorizationCodeAuth auth = new AutorizationCodeAuth(); auth.ClientId = DataAccess.GetRocolappData().ClientId; auth.RedirectUri = DataAccess.GetRocolappData().RedirectUri; string clientSecret = DataAccess.GetRocolappData().ClientSecret; Token newToken = auth.RefreshToken(refreshToken, DataAccess.GetRocolappData().ClientSecret); return(newToken); }
public void Authenticate() { AutorizationCodeAuth authentication = new AutorizationCodeAuth { RedirectUri = new UriBuilder("http://127.0.0.1") { Port = 7476 }.Uri.OriginalString.TrimEnd('/'), ClientId = _clientId, Scope = _scope, State = "XSS" }; // Try refreshing var token = authentication.RefreshToken(Properties.Settings.Default.RefreshToken, _clientSecret); if (token.Error == null) { WebAPI = ApiFromToken(token); return; } AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false); WebAPI = null; authentication.OnResponseReceivedEvent += (response) => { WebAPI = HandleSpotifyResponse(response, authentication); authenticationWaitFlag.Set(); }; try { authentication.StartHttpServer(7476); authentication.DoAuth(); authenticationWaitFlag.WaitOne(TimeSpan.FromSeconds(_timeout)); if (WebAPI == null) { throw new TimeoutException($"No valid response received for the last {_timeout} seconds"); } } finally { authentication.StopHttpServer(); } }
public async Task <SearchItem> SearchTrack(string query, string tk) { RoclappData data = DataAccess.GetRocolappData(); RocolappUser user = GetUserbyRocolappId(tk); var spotify = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = user.Token, UseAuth = true }; SearchItem searchItem = spotify.SearchItems(query, SearchType.Track); if (searchItem.Error != null && searchItem.Error.Status == 401) { AutorizationCodeAuth auth = new AutorizationCodeAuth(); //Datos de mi aplicacion Rocolapp auth.ClientId = DataAccess.GetRocolappData().ClientId; auth.RedirectUri = DataAccess.GetRocolappData().RedirectUri; string clientSecret = DataAccess.GetRocolappData().ClientSecret; Token token = auth.RefreshToken(user.RefreshToken, DataAccess.GetRocolappData().ClientSecret); UpdateToken(user.Id.ToString(), token.AccessToken); var withRefreshedToken = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = token.AccessToken, UseAuth = true }; searchItem = withRefreshedToken.SearchItems(query, SearchType.Track); } return(searchItem); }
public void AddTrackToPlayList([FromBody] Track track) { RoclappData data = DataAccess.GetRocolappData(); string token; RocolappUser user = GetUserbyRocolappId(track.Token); token = user.Token; var spotify = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = user.Token, UseAuth = true, }; //Agregar la posibilidad de que se aparezca la letra de la cancion string trackPrefix = "spotify:track:"; ErrorResponse response = spotify.AddPlaylistTrack(user.SpotifyId, user.PlaylistId, trackPrefix + track.TrackId); if (response.HasError() && response.Error.Status == 401) { AutorizationCodeAuth auth = new AutorizationCodeAuth(); //Datos de mi aplicacion Rocolapp auth.ClientId = DataAccess.GetRocolappData().ClientId; auth.RedirectUri = DataAccess.GetRocolappData().RedirectUri; string clientSecret = DataAccess.GetRocolappData().ClientSecret; Token newToken = auth.RefreshToken(user.RefreshToken, DataAccess.GetRocolappData().ClientSecret); UpdateToken(user.Id.ToString(), newToken.AccessToken); token = newToken.AccessToken; var withRefreshedToken = new SpotifyWebAPI() { TokenType = "Bearer", AccessToken = newToken.AccessToken, UseAuth = true }; response = withRefreshedToken.AddPlaylistTrack(user.SpotifyId, user.PlaylistId, trackPrefix + track.TrackId); } PlaybackContext context = GetCurrentPlayBack(token); if (context != null && !context.IsPlaying) { try { ResumePlayback(token); } catch (Exception) { throw; } } }