Send SMS with Google Voice

Home / Send SMS with Google Voice

As I’m moving more code that I’ve written in the past to my blog, I remembered I posted this code a long while on CodePaste. It allows you to send an SMS text message via your Google Voice account.

It’s a simple bit of code that’s authenticating via your Google account and then getting the proper auth codes in order to send the text message.


Honestly, I don’t know for sure if it still works since Google has been making changes to their authentication services and such, but here’s the code for posterity’s sake.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Dynamic;
using System.Collections;
using System.Collections.ObjectModel;
 
namespace SMSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string authUrl = @"https://www.google.com/accounts/ClientLogin";
            string authVoiceUrl = @"https://www.google.com/voice";
            string smsUrl = @"https://www.google.com/voice/sms/send/";
 
            try
            {
                // First, let's get the auth code - requires Google credentials
                StringBuilder sb = new StringBuilder();
                NameValueCollection kvp = new NameValueCollection();
                kvp.Add("accountType", "GOOGLE");
                kvp.Add("Email", "your email here");
                kvp.Add("Passwd", "your password here");
                kvp.Add("service", "grandcentral");
                kvp.Add("source", "long2know.com-test-1.0");
 
                foreach (string key in kvp.Keys)
                {
                    sb.Append(string.Format("{0}={1}&", key, kvp[key]));
                }
 
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(sb.ToString());
 
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(authUrl);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
 
                StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream());
                string readResponse = sr.ReadToEnd();
 
                // Parse SID, Auth, LSID
                Regex regAuth = new Regex(@"Auth=(.+)", RegexOptions.IgnoreCase);
                Regex regSid = new Regex(@"SID=(.+)", RegexOptions.IgnoreCase);
                Regex regLsid = new Regex(@"LSID=(.+)", RegexOptions.IgnoreCase);
                Regex regRnrse = new Regex(@"'_rnr_se': '([^']+)'", RegexOptions.IgnoreCase);
 
                Match matchAuth = regAuth.Match(readResponse);
                Match matchSid = regSid.Match(readResponse);
                Match matchLsid = regLsid.Match(readResponse);
 
                string auth = string.Empty;
                string sid = string.Empty;
                string lsid = string.Empty;
                string rnrse = string.Empty;
 
                if (matchAuth.Success && matchSid.Success && matchLsid.Success)
                {
                    auth = matchAuth.Groups[0].Value;
                    sid = matchSid.Groups[0].Value;
                    lsid = matchLsid.Groups[0].Value;
                }
                else
                {
                    throw new WebException("Could not authenticate.", new Exception("Failed to retrieve auth, sid, or lsid"));
                }
 
                // Now, we need to get the rnr_se code
                myRequest = (HttpWebRequest)WebRequest.Create(authVoiceUrl);
                myRequest.Headers.Add(HttpRequestHeader.Authorization, string.Format("GoogleLogin {0}", auth));
 
                sr = new StreamReader(myRequest.GetResponse().GetResponseStream());
                readResponse = sr.ReadToEnd();
 
                Match matchRnrse = regRnrse.Match(readResponse);
 
                if (matchRnrse.Success)
                {
                    rnrse = matchRnrse.Groups[1].Value;
                }
                else
                {
                    throw new WebException("Could not authenticate.", new Exception("Failed to retrieve rnr_se"));
                }
 
                // Finally, let's send the message
                sb = new StringBuilder();
                kvp = new NameValueCollection();
                kvp.Add("_rnr_se", System.Web.HttpUtility.UrlEncode(rnrse));
                kvp.Add("phoneNumber", "18885551010"); // country code + area code + phone number (international notation)
                kvp.Add("text", System.Web.HttpUtility.UrlEncode("Here is my test SMS a console app!"));
                kvp.Add("id", "");
 
                foreach (string key in kvp.Keys)
                {
                    sb.Append(string.Format("{0}={1}&", key, kvp[key]));
                }
 
                data = encoding.GetBytes(sb.ToString());
 
                myRequest = (HttpWebRequest)WebRequest.Create(smsUrl);
                myRequest.Headers.Add(HttpRequestHeader.Authorization, string.Format("GoogleLogin {0}", auth));
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
 
                sr = new StreamReader(myRequest.GetResponse().GetResponseStream());
                readResponse = sr.ReadToEnd();
 
                // Deserialize into a dynamic type - uses DynamicJsonConverter (courtesy of this post:  http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object)
                var serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
 
                dynamic resp = serializer.Deserialize(readResponse, typeof(object));
 
                // Alternatively, you use a simple dictionary dynamic type
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //var json = js.Deserialize<dynamic>(readResponse);
 
                if (resp.ok)
                {
                    Console.WriteLine("Message was sent successfully.");
                }
                else
                {
                    Console.WriteLine("Message was not sent successfully.");
                }
 
 
            }
            catch (WebException e)
            {
                Console.WriteLine(string.Format("Web Exception: {0}", e.InnerException));
            }
 
        }
    }
 
    public class DynamicJsonConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
 
            return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
        }
 
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }
 
        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
        }
 
        #region Nested type: DynamicJsonObject
 
        private sealed class DynamicJsonObject : DynamicObject
        {
            private readonly IDictionary<string, object> _dictionary;
 
            public DynamicJsonObject(IDictionary<string, object> dictionary)
            {
                if (dictionary == null)
                    throw new ArgumentNullException("dictionary");
                _dictionary = dictionary;
            }
 
            public override string ToString()
            {
                var sb = new StringBuilder("{");
                ToString(sb);
                return sb.ToString();
            }
 
            private void ToString(StringBuilder sb)
            {
                var firstInDictionary = true;
                foreach (var pair in _dictionary)
                {
                    if (!firstInDictionary)
                        sb.Append(",");
                    firstInDictionary = false;
                    var value = pair.Value;
                    var name = pair.Key;
                    if (value is string)
                    {
                        sb.AppendFormat("{0}:\"{1}\"", name, value);
                    }
                    else if (value is IDictionary<string, object>)
                    {
                        new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
                    }
                    else if (value is ArrayList)
                    {
                        sb.Append(name + ":[");
                        var firstInArray = true;
                        foreach (var arrayValue in (ArrayList)value)
                        {
                            if (!firstInArray)
                                sb.Append(",");
                            firstInArray = false;
                            if (arrayValue is IDictionary<string, object>)
                                new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
                            else if (arrayValue is string)
                                sb.AppendFormat("\"{0}\"", arrayValue);
                            else
                                sb.AppendFormat("{0}", arrayValue);
 
                        }
                        sb.Append("]");
                    }
                    else
                    {
                        sb.AppendFormat("{0}:{1}", name, value);
                    }
                }
                sb.Append("}");
            }
 
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                if (!_dictionary.TryGetValue(binder.Name, out result))
                {
                    // return null to avoid exception.  caller can check for null this way...
                    result = null;
                    return true;
                }
 
                var dictionary = result as IDictionary<string, object>;
                if (dictionary != null)
                {
                    result = new DynamicJsonObject(dictionary);
                    return true;
                }
 
                var arrayList = result as ArrayList;
                if (arrayList != null && arrayList.Count > 0)
                {
                    if (arrayList[0] is IDictionary<string, object>)
                        result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)));
                    else
                        result = new List<object>(arrayList.Cast<object>());
                }
 
                return true;
            }
        }
 
        #endregion
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.