private int CreateUser(string token, string refreshToken, string spotifyId, string playlistId) { RocolappUser newUser = new RocolappUser(); newUser.PlaylistId = playlistId; newUser.Token = token; newUser.RefreshToken = refreshToken; newUser.SpotifyId = spotifyId; var userId = dataAccess.SaveRocolapUserData(newUser); return(userId); }
public int SaveRocolapUserData(RocolappUser rocolappUser) { try { context.RocolappUsers.Add(rocolappUser); var id = context.SaveChanges(); return(id); } catch (Exception e) { throw e; } }
// GET: MainPage public ActionResult Main(string code) { AutorizationCodeAuth auth = new AutorizationCodeAuth(); RocolappUser rocolapUser; //Datos de mi aplicacion Rocolapp auth.ClientId = DataAccess.GetRocolappData().ClientId; auth.RedirectUri = DataAccess.GetRocolappData().RedirectUri; string clientSecret = DataAccess.GetRocolappData().ClientSecret; Token token = auth.ExchangeAuthCode(code, clientSecret); PrivateProfile privateProfile = GetUserProfile(token.AccessToken); var existence = CheckIfUserExists(privateProfile.Id); if (existence) { var user = GetUser(privateProfile.Id); rocolapUser = new RocolappUser() { Id = user.Id, SpotifyId = user.SpotifyId, Token = user.Token, RefreshToken = user.RefreshToken, PlaylistId = user.PlaylistId, Place = user.Place }; } else { FullPlaylist playList = CreatePlayList(privateProfile.Id, token.AccessToken); int ok = CreateUser(token.AccessToken, token.RefreshToken, privateProfile.Id, playList.Id); rocolapUser = new RocolappUser() { Id = ok, SpotifyId = privateProfile.Id, Token = token.AccessToken, RefreshToken = token.RefreshToken, PlaylistId = playList.Id }; } ViewData["pubUser"] = rocolapUser; return(View()); }
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; } } }