public TransportService(string url = null) { if (url != null) { Transporter = new Transporter(url); } else { Transporter = new Transporter(TwitchUrl); } }
public static void CheckForSubscriber(string url) { var transporter = new Transporter("TODO get code and save it"); var test = TryTwitchRequest(transporter); }
// starta pumpandet på api:et public static WebResponse TryTwitchRequest(Transporter transporter, string url = null) { var postdata = "client_id=" + transporter.ClientId + "&client_secret=" + transporter.SecretId + "&grant_type=authorization_code" + "&redirect_uri=" + transporter.Redirect_Uri + "&code=" + transporter.Code + transporter.State != null ? "&state=" + transporter.State : string.Empty; var encoding = new ASCIIEncoding(); var postdataArray = encoding.GetBytes(postdata); var request = WebRequest.Create(url != null ? url : TwitchUrl); request.ContentLength = postdataArray.Length; request.Method = "POST"; //request.ContentType = "application/x-www-form-urlencoded"; try { var response = request.GetResponse(); response.ContentType = "application/json"; return response; } catch (WebException wex) { var httpResponse = wex.Response as HttpWebResponse; if (httpResponse != null) { throw new ApplicationException(string.Format( "Remote server call {0} {1} resulted in a http error {2} {3}.", request.Method, request.RequestUri, httpResponse.StatusCode, httpResponse.StatusDescription), wex); } else { throw new ApplicationException(string.Format( "Remote server call {0} {1} resulted in an error.", request.Method, request.RequestUri), wex); } } catch (Exception) { throw; } // för att kunna skicka med POST body URL encodade, måste vi använda en GetRequestStream. using (var requestStream = request.GetRequestStream()) { // vi får ju ett json objekt tillbaka från anropet till servern, hur sparar vi det? requestStream.Write(postdataArray, 0, postdataArray.Length); } }