Google语音通话错误

时间:2012-02-21 11:27:24

标签: asp.net google-api

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();
            }
        }
    }
}

我已经使用上述代码使用Google voicall服务拨打Googl语音电话,但我收到了错误消息。错误是 找不到_rnr_se键

pelase告诉我这是什么 m_gizmoNumber和_rnr_se键

1 个答案:

答案 0 :(得分:0)

在登录请求之前添加此行:

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

请参阅here