I am writing a business app on IOS and I need to sent preformed text messages when specific events happen. It looked like it would be reasonably easy to do with Twilio, so that is the direction I went down.
I wrote the follow helper in my Forms portable class.
public static class SMSHelper { /*source: http://stackoverflow.com/questions/10077237/httpclient-authentication-header-not-getting-sent * https://www.twilio.com/user/account/developer-tools/api-explorer/message-create */ public static async Task<bool> SendSMS(string to, string msg) { var url = "https://api.twilio.com/2010-04-01/Accounts/AC{account removed}/Messages.json"; var rVal = false; using (var handler = new HttpClientHandler()) { handler.Credentials = new System.Net.NetworkCredential("AC{Account removed}", "{secret removed}"); using (var client = new HttpClient(handler)) { var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("To", to), new KeyValuePair<string, string>("From", "(434) 234-4008"), new KeyValuePair<string, string>("Body", msg)//System.Net.WebUtility.UrlEncode(msg)), }); HttpResponseMessage response = client.PostAsync(url, requestContent).Result; rVal = response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK; var content = await response.Content.ReadAsStringAsync(); } } return rVal; } }
This works great from a testing console app that I wrote. The response is HttpStatusCode.OK and the sms gets sent as expected.
The problem is when I call the same code from within the portable class project (the same one that contains this class) that is used in my Xamarin projects it gets a response statuscode of Unauthorized.
That seems to point to the universal app encoding the handler.Credentials = new System.Net.NetworkCredential("AC{Account removed}", "{secret removed}"); line differently, but I am not really sure where to go from here.
TIA