public static string GetAccessToken(string ClientID, string ClientSecret)
        {
            SpotifyToken token = new SpotifyToken();
            string       url5  = "https://accounts.spotify.com/api/token";

            var encode_clientid_clientsecret = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", ClientID, ClientSecret)));

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url5);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            //  webRequest.Accept = "application/json";
            webRequest.Headers.Add("Authorization", "Basic " + encode_clientid_clientsecret);

            var request = ("grant_type=client_credentials&scope=playlist-modify-public playlist-modify-private");//

            byte[] req_bytes = Encoding.ASCII.GetBytes(request);
            webRequest.ContentLength = req_bytes.Length;

            using (Stream strm = webRequest.GetRequestStream())
            {
                strm.Write(req_bytes, 0, req_bytes.Length);
                strm.Close();
            }

            string Json;

            using (HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse())
                using (Stream respStr = resp.GetResponseStream())
                    using (StreamReader Reader = new StreamReader(respStr, Encoding.UTF8))
                    {
                        Json = Reader.ReadToEnd();
                        //should get back a string i can then turn to json and parse for accesstoken
                        Reader.Close();
                    }

            token = JsonConvert.DeserializeObject <SpotifyToken>(Json);
            return(token.Access_token);
        }
Beispiel #2
0
        public string SearchAlbumsTracks(string Word)
        {
            //https://api.spotify.com/v1/albums?ids
            string url     = string.Format("https://api.spotify.com/v1/albums/{0}/tracks", Word);
            string Request = string.Empty;
            string myToken = SpotifyToken.GetAccessToken("bd09b0277ece4ad1b30955a0b51c6101", "dac122b7bc304e909636cf52c0c5a037");

            try
            {
                // Get token for request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.Accept = "application/json";
                request.Headers.Add("Authorization", "Bearer " + myToken);

                string Json;

                using (WebResponse response = request.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                        using (StreamReader Reader = new StreamReader(stream))
                        {
                            Json = Reader.ReadToEnd();
                            Reader.Close();
                        }

                return(Json);
            }
            catch (WebException ex)
            {
                Console.WriteLine("Track Request Error: " + ex.Status);
            }
            catch (TaskCanceledException tex)
            {
                Console.WriteLine("Track Request Error: " + tex.Message);
            }

            return(null);
        }
Beispiel #3
0
        public string SearchTrack(string Word)
        {
            string url   = "https://api.spotify.com/v1/search?q=" + Word + "&type=track";
            string token = SpotifyToken.GetAccessToken("bd09b0277ece4ad1b30955a0b51c6101", "dac122b7bc304e909636cf52c0c5a037");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method      = "GET";
            request.ContentType = "application/json";
            request.Accept      = "application/json";
            request.Headers.Add("Authorization", "Bearer " + token);

            string Json;

            using (WebResponse response = request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader Reader = new StreamReader(stream))
                    {
                        Json = Reader.ReadToEnd();
                        Reader.Close();
                    }

            return(Json);
        }