lv211 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to log in to blogger, but I keep on getting the login screen. I think this might have something to do with cookies.

I noticed there is a redirect page with javascript on it that redirects to the main page. I can't seem to get around this.

Here is my script. Any help or insight would be greatly appreciated.

#!/usr/bin/perl use WWW::Mechanize; use HTTP::Cookies; my $email = " "; my $password = " "; my $url = "https://www.google.com/accounts/ServiceLoginBox?service=blo +gger&continue=http%3A%2F%2Fwww.blogger.com%2Floginz%3Fd%3Dhttp%25 +3A%252F%252Fwww.blogger.com&alwf=true&uilel=3&skipvpage=t +rue&rm=false&naui=8&showra=1&fpui=2&hl=en&nui +=1&naui=8&roec=Unknown&alwf=true&service=blogger& +continue=http%3A%2F%2Fwww.blogger.com%2Floginz%3Fd%3D%252Fstart&f +pui=2&rm=false&alinsu=1&uilel=3&hl=en&skipvpage=t +rue"; my $mech = WWW::Mechanize->new; my $cookie_jar = HTTP::Cookies->new( 'file' => 'c:/lwp_cookies.dat', ); $mech->agent('Mozilla/5.0(Windows; U; Windows NT 5.1; en-US) Gecko/200 +61206 Firefox/1.5.0.9'); $mech->cookie_jar($cookie_jar); $mech->get($url); $mech->set_fields( "Email" => "$email", "Passwd" => "$password", ); $mech->submit; print $mech->content;

Replies are listed 'Best First'.
Re: WWW::Mechanize login problems
by naikonta (Curate) on May 28, 2007 at 01:21 UTC
    Hi, lv211, you may see my answer you might find relevant to your problem. I, however, use the basic LWP::UserAgent and others but I can't see why it can't be migrated to WWW::Mechanize.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: WWW::Mechanize login problems
by naikonta (Curate) on May 28, 2007 at 05:35 UTC
    I happened to notice that you mention about blogger. I think I've just given you the wrong direction. Instead, I'd like to suggest that the best thing to do is learning the API. And you sure want to read about the authentication process which also applies to other Google services.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: WWW::Mechanize login problems
by neverminda (Novice) on May 28, 2007 at 04:34 UTC
    Maybe you should try to login to your blog at blogger.com if you only want to get into your blog.
Re: WWW::Mechanize login problems
by moritz (Cardinal) on May 28, 2007 at 12:24 UTC
    I don't think you should encode ampersands as &, that's only necessary when they are embedded in HTML code.
Re: WWW::Mechanize login problems
by lv211 (Beadle) on May 28, 2007 at 21:42 UTC
    @neverminda - Obviously I'm going to do more than just log on to my blogger account.

    @naikonta - I was thinking about using the API, and I might have to if I don't solve this problem. Thanks for your input.

    @moritz - That url string is taken from an iframe. If you just use $mech->get('http://www.blogger.com/'); you will find that google has hidden the login form in an iframe. I just took the url from that login and put it into a scalar variable.

    The redirect is a javascript redirect. This is what it looks like (and where I've gotten stuck).
    <html> <head> <script language="JavaScript" type="text/javascript"> <!-- function doRedirect(url) { open(url, "_top"); } --> </script> <script language="JavaScript1.4" type="text/javascript"> <!-- function doRedirect(url) { try { open(url, "_top"); } catch (xcp) { insertContinueLink(); } } --> </script> <script language="EcmaScript" type="text/javascript"> <!-- function doRedirect(url) { try { open(url, "_top"); } catch (xcp) { insertContinueLink(); } } --> </script> <script type="text/javascript"> <!-- var firsttime = "firsttime"; var goback = "goback"; var gofwd = "gofwd"; function redirect() { var url = 'https:\x2F\x2Fwww.google.com\x2Faccounts\x2FCheckCook +ie?service=blogger&chtml=LoginDoneHtml'; var state = document.dummyform.dummyfield.value; var lastUrl = document.dummyform.dummyfield2.value; if ((state == firsttime) || (url != lastUrl)) { // First time visit or passive login document.dummyform.dummyfield.value = goback; document.dummyform.dummyfield.defaultvalue = goback; document.dummyform.dummyfield2.value = url; document.dummyform.dummyfield2.defaultvalue = url; doRedirect(url); } else if (state == goback) { // Remember that we should go forward next time and go backwar +d. document.dummyform.dummyfield.value = gofwd; document.dummyform.dummyfield.defaultvalue = gofwd; history.back(); } else if (state == gofwd) { // Remember that we should go backward next time and go forwar +d. document.dummyform.dummyfield.value = goback; document.dummyform.dummyfield.defaultvalue = goback; history.forward(); } } // Tells the user we are waiting for the page to redirect function insertLoadingMessage() { document.getElementById("loadingDiv").style.visibility = "visibl +e"; } function insertContinueLink() { document.getElementById("loadingDiv").style.visibility = "hidden +"; document.getElementById("clickToContinue").style.visibility = "v +isible"; } --> </script> <style type="text/css"><!-- body { color:white; background:#F5EDE3; font-size: 12; font-family:"Trebuchet MS"; } #loadingDiv { float:left; background:#826C55; margin:10px 0 0 10px; } a { color: white; } --></style> <script type="text/javascript"><!-- if (document.referrer.indexOf('nui=8') >= 0) { document.write('<style type="text/css">'); document.write('#body { background: #036 }'); document.write('#body #loadingDiv { background: none }'); document.write('</style>'); } // --> </script> </head> <body onload="insertLoadingMessage();redirect();" id="body" dir="ltr" onpageshow="if (event.persisted) redirect();"> <div id="loadingDiv" style="visibility:hidden;"> Loading... </div> <div style="visibility:hidden;" id="clickToContinue"> <a target="_top" href="https://www.google.com/accounts/CheckCookie?s +ervice=blogger&chtml=LoginDoneHtml">Click here to continue</a> </div> <form name="dummyform" action="" > <input name="dummyfield" value="firsttime" style="visibility:hidden" + /> <input name="dummyfield2" value="" style="visibility:hidden" /> </form> <noscript> Please <a href="https://www.google.com/accounts/CheckCookie?service=blogger +&chtml=LoginDoneHtml" target="_top">click here to continue</a>. </noscript> </body> </html>

    I've tried to do a $mech->follow_link( text => 'click here to continue'); but that just takes you in a circle.

    I also tried using a regular expression to take out "https:\x2F\x2Fwww.google.com\x2Faccounts\x2FCheckCook +ie?service=blogger&chtml=LoginDoneHtml" (the redirect url), but I get a "500 Can't connect to :443 (Bad hostname '')" error.
      I was thinking about using the API, and I might have to if I don't solve this problem.
      The problem is, reverse engineering tasks are so tedious, and tricky. They provide API so people can develop cleaner way to access their web services. But, well, all the tools are in your hand :-)
      https:\x2F\x2Fwww.google.com\x2Faccounts\x2FCheckCook +ie?service=blogger&chtml=LoginDoneHtml
      I think the \x2F is the way JavaScript encodes normal slash (/). If I rewrite it like:
      https://www.google.com/accounts/CheckCookie?service=blogger&chtml=Logi +nDoneHtml
      I get a response that says: Your browser's cookie functionality is turned off. Please turn it on. If I'm currently logged in to one of the services, the response I get is my account page. I think you're halfway there.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!