1

there is a bit of code I'm trying to replicate in Perl using either LWP::UserAgent or WWW::Mechanize from an existing script.

The original script actually does more than I'm looking to do. I'd just like to log into the Nest website (the part I need help with) and then parse out some data for historical logging (I'm good there).

My current script I would expect to work, but I'm not sure if the authResult/access_token from the Ruby example us actually understood/used by either Perl module.

My code in Perl:

#!/usr/bin/perl

use WWW::Mechanize;
#use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
use LWP::UserAgent;
use Data::Dumper;
use CGI;

my $email; #stores our mail
my $password; #stores our password
my $user_agent = 'Nest/1.1.0.10 CFNetwork/548.0.4';

$email = "email@email";
$password = "mypassword";

my @headers = (
   'User-Agent' => 'Nest/1.1.0.10 CFNetwork/548.0.4',
   'X-nl-user-id' => $email, 
   'X-nl-protocol-version' => '1', 
   'Accept-Language' => 'en-us', 
   'Connection' => 'keep-alive', 
   'Accept' => '*/*'
  );

# print "Content-type: text/html\n\n";

my $cookie = HTTP::Cookies->new(file => 'cookie',autosave => 1,);

my $browser = WWW::Mechanize->new(cookie_jar => $cookie, autocheck => 1,);

# tell it to get the main page
$browser->get("https://home.nest.com/user/login");

print Dumper($browser->forms);

# okay, fill in the box with the name of the
# module we want to look up
$browser->form_number(1);
$browser->field("username", $email);
$browser->field("password", $password);
$browser->submit();
print $browser->content();

When I submit the form, I just get the same page returned back to me, and I don't know what exactly is causing Nest to not like what I'm submitting. There are two additional fields in the form on their log-in page:

         'inputs' => [
                       bless( {
                                'maxlength' => '75',
                                '/' => '/',
                                'value_name' => 'E-mail address',
                                'name' => 'username',
                                'id' => 'id_username',
                                'type' => 'text'
                              }, 'HTML::Form::TextInput' ),
                       bless( {
                                '/' => '/',
                                'value_name' => 'Password',
                                'name' => 'password',
                                'id' => 'id_password',
                                'type' => 'password',
                                'minlength' => '6'
                              }, 'HTML::Form::TextInput' ),
                       bless( {
                                'readonly' => 1,
                                '/' => '/',
                                'value_name' => '',
                                'value' => '',
                                'name' => 'next',
                                'type' => 'hidden'
                              }, 'HTML::Form::TextInput' ),
                       bless( {
                                'readonly' => 1,
                                '/' => '/',
                                'value_name' => '',
                                'value' => 'dbbadca7910c5290a13d30785ac7fb79',
                                'name' => 'csrfmiddlewaretoken',
                                'type' => 'hidden'
                              }, 'HTML::Form::TextInput' )

Do I need to use the csrfmiddlewaretoken value in each submission? It appears to change. I thought getting a cookie upon a successful login would be enough.

Any suggestions on what I'm doing wrong?

David
  • 11
  • 2
  • 1
    Unless the site actually insists on a custom client, this little code should work: `use WWW::Mechanize qw(); my $w = WWW::Mechanize->new; $w->get('https://home.nest.com/accounts/login/'); $w->submit_form(with_fields => { username => 'theusername@example', password => 'thepassword' });` I do not have an account, so I cannot test. - If it fails, use Wireshark and record a successful HTTP transaction and a failing one from Mechanize and compare to find the exact differences. It probably goes quicker to wait someone who can translate from Ruby. – daxim Feb 21 '12 at 22:04

1 Answers1

0

Shot in the blue:

perl -E'use warnings; $email = "email@email"; say "<$email>"'
Possible unintended interpolation of @email in string at -e line 1.
<email>

I suspect it fails because the form gets the wrong user name, print it out to confirm. Always enable the pragmas strict and warnings to make many common mistakes visible.

Community
  • 1
  • 1
daxim
  • 39,270
  • 4
  • 65
  • 132