예제 #1
0
        public async Task RefreshAccessToken()
        {
            if (_spotifyAuthorizationDto == null)
            {
                throw new InvalidOperationException("Authorization object is null");
            }

            var requestBody = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("grant_type", "refresh_token"),
                new KeyValuePair <string, string>("refresh_token", _spotifyAuthorizationDto.Refresh_Token),
            };

            using (var httpClient = new HttpClient())
            {
                var msg      = tokenRequestMessage(requestBody);
                var response = await httpClient.SendAsync(msg);

                if (!response.IsSuccessStatusCode)
                {
                    throw new AuthenticationException($"Request for access token from Spotify failed with response: {await response.Content.ReadAsStringAsync()}");
                }

                _tokenAcquisitionTime    = DateTime.Now;
                _spotifyAuthorizationDto = await response.Content.ReadAsAsync <SpotifyAuthorizationDto>();
            }
        }
예제 #2
0
        public async Task AcquireAccessToken(string accessCode)
        {
            var requestBody = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("code", accessCode),
                new KeyValuePair <string, string>("redirect_uri", _config["AfterAuthRedirectUrl"])
            };

            using (var httpClient = new HttpClient())
            {
                var msg      = tokenRequestMessage(requestBody);
                var response = await httpClient.SendAsync(msg);

                if (!response.IsSuccessStatusCode)
                {
                    throw new AuthenticationException($"Request for access token from Spotify failed with response: {await response.Content.ReadAsStringAsync()}");
                }

                _tokenAcquisitionTime    = DateTime.Now;
                _spotifyAuthorizationDto = await response.Content.ReadAsAsync <SpotifyAuthorizationDto>();
            }
        }