2012-02-21 98 views
0
namespace GoogleVoiceCall 
{ 
    class Program 
    { 

     private const string LOGIN_URL = "https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral"; 
     private const string GOOGLE_VOICE_HOME_URL = "https://www.google.com/voice"; 
     private const string CALL_URL = "https://www.google.com/voice/call/connect"; 

     private static string m_emailAddress = "your email address"; 
     private static string m_password = "password"; 
     private static string m_gizmoNumber = "your gizmo number"; 
     private static string m_destinationNumber = "your destination number"; 

     static void Main(string[] args) 
     { 
      try 
      { 
       Console.WriteLine("Attempting Google Voice Call"); 

       CookieContainer cookies = new CookieContainer(); 

       // First send a login request to get the necessary cookies. 
       string loginData = "Email=" + Uri.EscapeDataString(m_emailAddress) 
         + "&Passwd=" + Uri.EscapeDataString(m_password); 
       HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LOGIN_URL); 
       loginRequest.CookieContainer = cookies; 
       loginRequest.AllowAutoRedirect = true; 
       loginRequest.Method = "POST"; 
       loginRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; 
       loginRequest.ContentLength = loginData.Length; 
       loginRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(loginData), 0, loginData.Length); 

       HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse(); 
       if (loginResponse.StatusCode != HttpStatusCode.OK) 
       { 
        throw new ApplicationException("Login failed."); 
       } 
       else 
       { 
        Console.WriteLine("Login request was successful."); 
       } 

       // Second send a request to the Google Voice home page to get a string key needed when placing a callback. 
       HttpWebRequest keyRequest = (HttpWebRequest)WebRequest.Create(GOOGLE_VOICE_HOME_URL); 
       keyRequest.CookieContainer = cookies; 

       HttpWebResponse keyResponse = (HttpWebResponse)keyRequest.GetResponse(); 
       if (keyResponse.StatusCode != HttpStatusCode.OK) 
       { 
        throw new ApplicationException("_rnr_se key request failed."); 
       } 
       else 
       { 
        Console.WriteLine("Key request was successful."); 
       } 

       StreamReader reader = new StreamReader(keyResponse.GetResponseStream()); 
       string keyResponseHTML = reader.ReadToEnd(); 
       Match rnrMatch = Regex.Match(keyResponseHTML, @"name=""_rnr_se"".*?value=""(?<rnrvalue>.*?)"""); 
       if (!rnrMatch.Success) 
       { 
        throw new ApplicationException("_rnr_se key was not found on your Google Voice home page."); 
       } 
       string rnr = rnrMatch.Result("${rnrvalue}"); 
       Console.WriteLine("_rnr_se key=" + rnr); 

       // Thirdly (and lastly) submit the request to initiate the callback. 
       string callData = "outgoingNumber=" + Uri.EscapeDataString(m_destinationNumber) + 
      "&forwardingNumber=" + Uri.EscapeDataString(m_gizmoNumber) + 
      "&subscriberNumber=undefined&remember=0&_rnr_se=" + Uri.EscapeDataString(rnr); 
       HttpWebRequest callRequest = (HttpWebRequest)WebRequest.Create(CALL_URL); 
       callRequest.CookieContainer = cookies; 
       callRequest.Method = "POST"; 
       callRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; 
       callRequest.ContentLength = callData.Length; 
       callRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(callData), 0, callData.Length); 

       HttpWebResponse callResponse = (HttpWebResponse)callRequest.GetResponse(); 
       if (callResponse.StatusCode != HttpStatusCode.OK) 
       { 
        Console.WriteLine("Call request failed."); 
       } 
       else 
       { 
        Console.WriteLine("Call request was successful."); 
       } 
      } 
      catch (Exception excp) 
      { 
       Console.WriteLine("Exception Main. " + excp.Message); 
      } 
      finally 
      { 
       Console.WriteLine("finished, press any key to exit..."); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 

我已经使用上述代码打电话使用谷歌voicall服务的Googl语音通话,但我收到一个错误。错误是 _rnr_se键没有被发现谷歌语音通话错误

pelase告诉这里是什么这个 m_gizmoNumber和_rnr_se关键

回答

0

登录请求之前加入这一行:

m_cookies.Add(new Uri(PRE_LOGIN_URL), galxResponse.Cookies); 

看到here