public PlaylistGenerator() { string clientId = ConfigurationManager.AppSettings[Constants.SpotifyClientIdSettingsName]; if (string.IsNullOrWhiteSpace(clientId)) { clientId = Environment.GetEnvironmentVariable(Constants.SpotifyClientIdSettingsName); } string clientSecret = ConfigurationManager.AppSettings[Constants.SpotifyClientSecretSettingsName]; if (string.IsNullOrWhiteSpace(clientSecret)) { clientSecret = Environment.GetEnvironmentVariable(Constants.SpotifyClientSecretSettingsName); } this.spotifyAuthorization = new SpotifyAuthorization(clientId, clientSecret); }
public string AuthorizeSpotify() { try { SpotifyAuthorization auth = new SpotifyAuthorization(); WebRequest request = WebRequest.Create("https://accounts.spotify.com/api/token"); request.Method = "POST"; string clientId = Environment.GetEnvironmentVariable("clientId"); string clientSecret = Environment.GetEnvironmentVariable("clientSecret"); var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(clientId + ":" + clientSecret); var encoded64 = System.Convert.ToBase64String(plainTextBytes); request.Headers.Set("Authorization", "Basic" + " " + encoded64); request.ContentType = "application/x-www-form-urlencoded"; var postData = "grant_type=client_credentials"; var data = Encoding.ASCII.GetBytes(postData); request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseFromServer = reader.ReadToEnd(); auth = JsonConvert.DeserializeObject <SpotifyAuthorization>(responseFromServer); } } return(auth.AccessToken); } catch (WebException ex) { return(ex.Message); } catch (Exception ex) { return(ex.Message); } }