public SpotifyClient(SpotifyAccessCredentials creds) { Credentials = creds; User = GET <SpotifyUser>(new SpotifyUserRequest()); if (!String.IsNullOrEmpty(User.Error)) { Console.WriteLine("We have some errors"); } else { Connected = true; } }
/// <summary> /// Method call for a GET Request /// </summary> /// <param name="EndPoint">In the form of /v1/artist</param> /// <param name="Query">In the form of ?key=value</param> public T GET <T>(ISpotifyRequest Request) { T ReturnResult = default(T); string Endpoint = UpdateEndpoint(Request.Endpoint + Request.Query); //Sending the HttpRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BaseAddress + Endpoint); request.Method = "GET"; request.Headers.Add("Authorization", "Bearer " + Credentials.Access_Token); HttpWebResponse response = null; //Processing the response try { using (response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { StreamReader sr = new StreamReader(response.GetResponseStream()); string JSonString = sr.ReadToEnd(); ReturnResult = JsonConvert.DeserializeObject <T>(JSonString); } } } catch (WebException E) { using (WebResponse error = E.Response) { HttpWebResponse httpResponse = (HttpWebResponse)error; if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) { //If unauthorised - refresh once and continue. connects += 1; if (connects == 1) { Credentials = SpotifyAuthoriser.Refresh(Credentials); return(GET <T>(Request)); } } } } return(ReturnResult); }
public static SpotifyAccessCredentials Refresh(SpotifyAccessCredentials Creds) { string ClientID = System.Configuration.ConfigurationManager.AppSettings["SpotifyClientID"]; string ClientSecret = System.Configuration.ConfigurationManager.AppSettings["SpotifyClientSecret"]; SpotifyAccessCredentials credentials = null; using (var client = new HttpClient()) { client.BaseAddress = BaseAddress; var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "refresh_token"), new KeyValuePair <string, string>("refresh_token", Creds.Refresh_Token) }); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", ClientID, ClientSecret)))); try { var post = client.PostAsync("/api/token", content); post.Wait(); HttpResponseMessage response = post.Result; if (response.StatusCode == HttpStatusCode.OK) { var jsonstring = response.Content.ReadAsStringAsync(); jsonstring.Wait(); string jstring = jsonstring.Result; credentials = JsonConvert.DeserializeObject <SpotifyAccessCredentials>(jstring); } } catch (Exception e) { throw; } } return(credentials); }
public static SpotifyAccessCredentials Refresh(SpotifyAccessCredentials Creds) { string ClientID = System.Configuration.ConfigurationManager.AppSettings["SpotifyClientID"]; string ClientSecret = System.Configuration.ConfigurationManager.AppSettings["SpotifyClientSecret"]; SpotifyAccessCredentials credentials = null; using (var client = new HttpClient()) { client.BaseAddress = BaseAddress; var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "refresh_token"), new KeyValuePair<string, string>("refresh_token", Creds.Refresh_Token) }); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", ClientID, ClientSecret)))); try { var post = client.PostAsync("/api/token", content); post.Wait(); HttpResponseMessage response = post.Result; if (response.StatusCode == HttpStatusCode.OK) { var jsonstring = response.Content.ReadAsStringAsync(); jsonstring.Wait(); string jstring = jsonstring.Result; credentials = JsonConvert.DeserializeObject<SpotifyAccessCredentials>(jstring); } } catch (Exception e) { throw; } } return credentials; }