Esempio n. 1
0
        public static async Task LinkSpotifyAsync(
            SpotifyLinkUploadModel spotifyLinkUploadModel,
            Guid userId,
            IRepositoryManager repositoryManager,
            SpotifyAPICredentials spotifyAPICredentials)
        {
            try
            {
                var user = await repositoryManager.UserRepository.GetByIdAsync(userId);

                if (user != null)
                {
                    var spotifyAuthRequest = new AuthorizationCodeTokenRequest
                                             (
                        spotifyAPICredentials.ClientId,
                        spotifyAPICredentials.ClientSecret,
                        spotifyLinkUploadModel.Code,
                        new Uri($"https://{spotifyLinkUploadModel.CallbackUrl}/callback")
                                             );

                    var spotifyAuthResponse = await new OAuthClient().RequestToken(spotifyAuthRequest);
                    var spotifyToken        = spotifyAuthResponse.AccessToken;

                    var httpClient         = new HttpClient();
                    var spotifyUserRequest = new HttpRequestMessage(new HttpMethod("GET"), "https://api.spotify.com/v1/me");
                    spotifyUserRequest.Headers.TryAddWithoutValidation("Authorization", $"Bearer {spotifyToken}");

                    var response = await httpClient.SendAsync(spotifyUserRequest);

                    var responseString = await response.Content.ReadAsStringAsync();

                    var spotifyUserResponse = JsonConvert.DeserializeObject <SpotifyUserResponse>(responseString);

                    var userImages = new List <UserImage>();
                    foreach (var image in spotifyUserResponse.Images)
                    {
                        userImages.Add(new UserImage
                        {
                            UserId = user.Id,
                            Width  = image.Width != null ? (short)image.Width : null,
                            Height = image.Height != null ? (short)image.Height : null,
                            Url    = image.Uri
                        });
                    }

                    user.SpotifyRefreshToken = EncryptionService.EncryptString(spotifyAuthResponse.RefreshToken);
                    user.SpotifyUsername     = spotifyUserResponse.Id;
                    user.SpotifyMarket       = spotifyUserResponse.Country;
                    user.UserImages          = userImages;
                    user.LastUpdatedOn       = DateTime.Now;
                    user.LastUpdatedBy       = userId;

                    await repositoryManager.UserRepository.UpdateAsync(user);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> LinkSpotifyAsync(SpotifyLinkUploadModel spotifyLinkUploadModel)
        {
            try
            {
                if (await IsUserValidAsync())
                {
                    await SpotifyModel.LinkSpotifyAsync(spotifyLinkUploadModel, ExtractUserIdFromToken(), RepositoryManager, _spotifyAPICredentials);

                    return(Ok());
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (Exception err)
            {
                LogException(err);

                return(Problem());
            }
        }