Created
          October 6, 2009 21:13 
        
      - 
      
 - 
        
Save gnowk/203434 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
gnowk created this gist
Oct 6, 2009 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ --- GoogleVoiceCall.cs.1.1 2009-10-04 23:47:41.000000000 -0700 +++ GoogleVoiceCall.cs 2009-10-06 14:07:27.000000000 -0700 @@ -49,10 +49,11 @@ public class GoogleVoiceCall { + private const string PRE_LOGIN_URL = "https://www.google.com/accounts/ServiceLogin"; private const string LOGIN_URL = "https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral"; private const string VOICE_HOME_URL = "https://www.google.com/voice"; private const string VOICE_CALL_URL = "https://www.google.com/voice/call/connect"; - private const int WAIT_FOR_CALLBACK_TIMEOUT = 10; + private const int WAIT_FOR_CALLBACK_TIMEOUT = 15; private const int HTTP_REQUEST_TIMEOUT = 5; private static ILog logger = AppState.logger; @@ -114,8 +115,34 @@ try { Log_External(new SIPMonitorControlClientEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Logging into google.com for " + emailAddress + ".", m_username)); + // Fetch GALX + HttpWebRequest galxRequest = (HttpWebRequest)WebRequest.Create(PRE_LOGIN_URL); + galxRequest.ConnectionGroupName = "prelogin"; + galxRequest.CookieContainer = cookies; + + HttpWebResponse galxResponse = (HttpWebResponse)galxRequest.GetResponse(); + if (galxResponse.StatusCode != HttpStatusCode.OK) { + galxResponse.Close(); + throw new ApplicationException("Load of the Google Voice pre-login page failed with response " + galxResponse.StatusCode + "."); + } + else { + Log_External(new SIPMonitorControlClientEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Google Voice pre-login page loaded successfully.", m_username)); + } + + StreamReader galxReader = new StreamReader(galxResponse.GetResponseStream()); + string galxResponseFromServer = galxReader.ReadToEnd(); + galxResponse.Close(); + + Match galxMatch = Regex.Match(galxResponseFromServer, @"name=""GALX""\s+?value=""(?<galxvalue>.*?)"""); + if (galxMatch.Success) { + Log_External(new SIPMonitorControlClientEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "GALX key " + galxMatch.Result("${galxvalue}") + " successfully retrieved.", m_username)); + } + else { + throw new ApplicationException("Could not find GALX key on your Google Voice pre-login page, callback cannot proceed."); + } + // Build login request. - string loginData = "Email=" + Uri.EscapeDataString(emailAddress) + "&Passwd=" + Uri.EscapeDataString(password); + string loginData = "Email=" + Uri.EscapeDataString(emailAddress) + "&Passwd=" + Uri.EscapeDataString(password) + "&GALX=" + Uri.EscapeDataString(galxMatch.Result("${galxvalue}")); HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LOGIN_URL); loginRequest.CookieContainer = cookies; loginRequest.ConnectionGroupName = "login";