【问题标题】:Difficulties with google authentication谷歌认证的困难
【发布时间】:2011-01-29 10:53:03
【问题描述】:

我正在尝试使用以下代码对 google 进行身份验证,但 google 再次将我发送回登录页面

//STEP# 1
                string loginURL = "https://www.google.com/accounts/ServiceLoginBox?service=analytics&nui=1&hl=en-US&continue=https%3A%2F%2Fwww.google.com%2Fanalytics%2Fsettings%2F%3Fet%3Dreset%26hl%3Den%26et%3Dreset%26hl%3Den-US";
                request = (HttpWebRequest)WebRequest.Create(loginURL);
                request.CookieContainer = cookieJar;
                request.Method = "GET";             
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4";


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();


                foreach (Cookie cook in response.Cookies)
                {
                      cookieJar.Add(cook);
                }


                using (StreamReader sr = new StreamReader(response.GetResponseStream()) )
                {
                    serverResponse = sr.ReadToEnd();                       
                    sr.Close();
                }           

                galx = ExtractValue(serverResponse,"GALX","name=\"GALX\" value=\"");

                Console.WriteLine(galx);


                //Request# 2

                string uriWithData = "https://www.google.com/accounts/ServiceLoginBoxAuth";
                request = (HttpWebRequest)WebRequest.Create(uriWithData);
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4";
                request.Method = "POST";
                request.CookieContainer = cookieJar;
                string param = string.Format("Email={0}&Passwd={1}&continue={2}&service=analytics&nui=1&dsh=8209101995200094904&GALX={3}&hl=en-US&PersistentCookie=yes","**my email address**",p,"",galx);
                byte[] postArr = StrToByteArray(param);
                request.ContentType = @"application/x-www-form-urlencoded"; 
                request.ContentLength = param.Length;

                Stream reqStream = request.GetRequestStream();
                reqStream.Write(postArr,0,postArr.Length);
                reqStream.Close();

                response = (HttpWebResponse)request.GetResponse();


                foreach (Cookie cook in response.Cookies)
                {
                      cookieJar.Add(cook);
                }


                using (StreamReader sr = new StreamReader(response.GetResponseStream()) )
                {
                    serverResponse = sr.ReadToEnd();

                    Console.WriteLine(serverResponse);
                    // Close and clean up the StreamReader       
                    sr.Close();
                }           

【问题讨论】:

  • 你得到什么错误?在女巫点不工作?
  • 您收到什么样的异常/消息?什么不工作?
  • @Younes,@Aristos 它把我送回登录页面。

标签: c# httpwebrequest httpwebresponse


【解决方案1】:

尝试查看google accounts apiGBaseService。我相信你必须像 Marty 所做的那样在accountType 中设置HOSTED_OR_GOOGLE here in this post

【讨论】:

    【解决方案2】:

    我不知道,我很确定没有多少人想要筛选那么多代码。

    我一眼就看出可能会导致问题的一件事是您使用 cookie 的次数过多。

    创建一个 CookieContainer 并在每个请求中传递它。

    无需“转移”或“重新创建”容器。

    【讨论】:

    • 我只有一个 cookiecontainer 对象,每次请求都会发送它
    • @Sanders cookieJar 对象范围是全局的
    • @user283405 - 你看到的任何地方cookieJar.Add(cook) 就是我所说的。这不仅是不必要的,而且可能是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2014-02-22
    • 2011-05-11
    相关资源
    最近更新 更多