/// <summary>
        /// Service responsible to Request Spotify Token Authorization
        /// </summary>
        public async Task <string> GetTokenAuthorizationAsync()
        {
            SpotifyAuthorizationResponse spotifyAuthorizationResponse = null;

            try
            {
                string clientIdSpotify     = WorkBench.Configuration.GetSection("ClientIdSpotify").Value;
                string clientSecretSpotify = WorkBench.Configuration.GetSection("ClientSecretSpotify").Value;
                string spotifyIdEncode     = Encode(clientIdSpotify + ":" + clientSecretSpotify);

                Dictionary <string, string> header = new Dictionary <string, string>();
                header.Add("Authorization", "Basic " + spotifyIdEncode);
                header.Add("Content-Type", "application/x-www-form-urlencoded");

                string body = "grant_type=client_credentials";

                spotifyAuthorizationResponse = await new ApiWrapper("SpotifyAuthorization").PostAsync <SpotifyAuthorizationResponse>($"", body.ToJsonCamelCase(), header);
            }
            catch (Exception ex)
            {
                AddBusinessInfo("NO_INTERNET");
                Logger.Error(ex, ex.Message);
            }
            return(spotifyAuthorizationResponse?.AccessToken);
        }
Example #2
0
        private async Task <string> ProcessAuthorizationResponse(HttpResponseMessage responseMessage)
        {
            if (!responseMessage.IsSuccessStatusCode)
            {
                // TODO: error handling
                throw new Exception(await responseMessage.Content.ReadAsStringAsync());
            }
            SpotifyAuthorizationResponse response = JsonConvert.DeserializeObject <SpotifyAuthorizationResponse>(
                await responseMessage.Content.ReadAsStringAsync());

            AccessTokenExpiresAt = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn);
            if (response.RefreshToken != null)
            {
                RefreshToken = response.RefreshToken;
            }
            AccessToken = response.AccessToken;
            return(AccessToken);
        }
        private async Task <SpotifyAuthorizationResponse> GetAuthorizationResponseAsync()
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Base64Encode(ClientID + ":" + ClientSecret));

                var content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("grant_type", "client_credentials")
                });

                HttpResponseMessage response = await client.PostAsync(AuthorizationEndpoint, content);

                string jsonString = await response.Content.ReadAsStringAsync();

                SpotifyAuthorizationResponse authorizationResponse = JsonConvert.DeserializeObject <SpotifyAuthorizationResponse>(jsonString);

                return(authorizationResponse);
            }
        }