Perl中Nest Thermostat站点的POST Web身份验证(尝试从Ruby转换)

时间:2012-02-21 20:48:40

标签: perl web-scraping

我正在尝试使用LWP :: UserAgent或现有脚本中的WWW :: Mechanize在Perl中复制一些代码。

original script实际上比我想做的更多。我只想登录Nest网站(我需要帮助的部分),然后解析一些历史记录的数据(我很擅长)。

我希望能够使用我当前的脚本,但是我不确定是否我们实际理解/使用了Perl模块的Ruby示例中的authResult/access_token

我在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();

当我提交表单时,我只是将同一页面返回给我,我不知道究竟是什么导致Nest不喜欢我提交的内容。表单上的表单中还有两个字段:

         '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' )

我是否需要在每次提交中使用csrfmiddlewaretoken值?它似乎有所改变。我认为在成功登录后获取cookie就足够了。

有关我做错的任何建议吗?

1 个答案:

答案 0 :(得分:0)

以蓝色拍摄:

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

我怀疑它失败了,因为表单获取了错误的用户名,打印出来确认。 Always enable the pragmas strict and warnings to make many common mistakes visible.