private async Task <string> CreateToken(SpotifyTokens spotifyTokens) { _spotifyService.AccessToken = spotifyTokens.AccessToken; var me = await _spotifyService.GetMeAsync(); var claims = new[] { new Claim("spotifyAccessToken", spotifyTokens.AccessToken), new Claim("spotifyRefreshToken", spotifyTokens.RefreshToken), new Claim("spotifyUserId", me.Id), new Claim("spotifyUserDisplayName", me.DisplayName), new Claim("spotifyUserImageUrl", me.Images.FirstOrDefault()?.Url ?? ""), }; var jwtSettings = _configuration.GetSection("Jwt").Get <JwtSettings>(); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Secret)); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var jwtToken = new JwtSecurityToken( jwtSettings.Issuer, jwtSettings.Audience, claims, expires: DateTime.UtcNow.AddSeconds(spotifyTokens.ExpiresIn), signingCredentials: credentials ); return(new JwtSecurityTokenHandler().WriteToken(jwtToken)); }
public async Task OnGetAsync([FromQuery] SpotifyToken token, string message, string type = "success") { Token = token; ViewData["AlertMessage"] = message; ViewData["AlertType"] = type; IsLogged = !(token == null || string.IsNullOrEmpty(token?.AccessToken)); MeModel = await _spotifyService.GetMeAsync(Token); }
public async Task <ActionResult <SpotifyData> > GetExportAsync([FromQuery] SpotifyToken token) { var hasMore = false; var offset = 0; if (string.IsNullOrEmpty(token?.AccessToken)) { return(RedirectToAction(nameof(GetAuthorization))); } var data = new SpotifyData(); var user = await _spotifyService.GetMeAsync(token); if (string.IsNullOrEmpty(user.Id)) { return(RedirectToPage($"/Index", new { Message = "Invalid token, login!", Type = "warning" })); } data.UserId = user.Id; data.DisplayName = user.DisplayName; do { var playlists = await _spotifyService.GetPlaylistsAsync(token, offset); foreach (var playlist in playlists.Items) { if (!playlist.Owner.Id.Equals(user.Id)) { data.FollowPlaylists.Add(playlist.Uri); continue; } var playlistItem = new SpotifyPlaylist(playlist); var _continue = false; offset = 0; do { var playlistTracks = await _spotifyService.GetPlaylistsTracksAsync(token, playlist.Id, offset); if (playlistTracks.Total == 0) { _continue = true; break; } foreach (var playlistTrack in playlistTracks.Items) { if (playlistTrack.IsLocal) { continue; //ignore local } playlistItem.Tracks.Add(new SpotifyPlaylistTrack(playlistTrack.Track)); } hasMore = !string.IsNullOrEmpty(playlistTracks.Next); offset = playlistTracks.Offset + playlistTracks.Limit; } while (hasMore); if (_continue) { continue; } data.Playlists.Add(playlistItem); } hasMore = !string.IsNullOrEmpty(playlists.Next); offset = playlists.Offset + playlists.Limit; } while (hasMore); offset = 0; do { var tracks = await _spotifyService.GetTracksAsync(token, offset); foreach (var track in tracks.Items) { data.Tracks.Add(new SpotifyPlaylistTrack(track.Track)); } hasMore = !string.IsNullOrEmpty(tracks.Next); offset = tracks.Offset + tracks.Limit; } while (hasMore); return(data); }