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

Hi experts,

I have a site at: https://steamcommunity.com/login/home/?goto=market%2F which I would like to login to with a Perl script. I had previously used WWW::Mechanize but just found out the site uses javascript, specifically this javascript:

function DoLogin() { var form = document.forms['logon']; var username = form.elements['username'].value; username = username.replace( /[^\x00-\x7F]/g, '' ); // remove non- +standard-ASCII characters var password = form.elements['password'].value; password = password.replace( /[^\x00-\x7F]/g, '' ); // remove non- +standard-ASCII characters if ( g_bLoginInFlight || password.length == 0 || username.length = += 0 ) return; g_bLoginInFlight = true; $('login_btn_signin').hide(); $('login_btn_wait').show(); new Ajax.Request( 'https://steamcommunity.com/login/getrsakey/', { method: 'post', parameters: { username: username, donotcache: ( new Date().getTime() ) }, onSuccess: OnRSAKeyResponse, onException: function( req, e ) { throw e; } } ); }

To login (find the javascript here: https://steamcommunity.com/public/javascript/login.js?v=264288658&). The question I have is how do I login to this site, somebody has said I could still use Mechanize but I wondered if LWP::UserAgent and POST would work.

Thanks in advance for your time, Seb Morris

Replies are listed 'Best First'.
Re: Help logging into a website with LWP::UserAgent
by Corion (Patriarch) on May 18, 2014 at 17:26 UTC

      I have found this syntax: $ua->post( $url, \%form, $field_name => $value, ... ) how do i use this to input this into the user or password form sections

        Again, you will have to learn about what HTTP is, and how it relates to HTML.

        Maybe starting by looking at the site source code gives you an idea of how the parts interact, or maybe you are better off to use WWW::Mechanize and/or one of its Javascript-enabled variants.

      Thanks for the reply, I do know it can be done the problem is that I don't know how to input into the user and password boxes at the link I posted then click login. CPAN also seems to be down right now so i cant check on there.

Re: Help logging into a website with LWP::UserAgent
by aitap (Curate) on May 18, 2014 at 19:32 UTC

    Speaking of the login procedure, DoLogin() seems to ask for an "RSA key" from the server and do the real authentication in another function called OnRSAKeyResponse. So, after you get the key by something like $lwp->post('https://steamcommunity.com/login/getrsakey/', { username => $username, donotcache => time() }) (untested; you may need to decode the answer if it's JSON), you'll need to do something else as described in OnRSAKeyResponse() function somewhere in JavaScript files. RSA-related modules (Crypt::RSA?) might help with this.

    Edit: I should also note that this approach will fail after site developers update login interface, because it's not public API which they are required to maintain in a persistent way. Generally, it's wiser so search whether the site you want to use provides an API. Steam Community seems to have one: http://steamcommunity.com/dev. Does it fullfill your needs?