0

I have a script that scrapes my scores from an website. This website uses my Facebook account for logging in. Since a few days the script does not work anymore and I get the following error:

Cookies Required: Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.

I searched on this forum and I found a similar problem: I tryed the given solution but it did not work.

I have export my FaceBook login cookies.txt from Google Chrome with this plugin. https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg?hl=en and save the cookies.txt in the same folder as the script:

use WWW::Mechanize;
use use HTTP::Cookies::Netscape;
my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies::Netscape->new( file => $cookies.txt ) );
$mech->agent_alias('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0');
$mech->get("https://www.facebook.com/login.php");
$mech->submit_form(
fields => {
    email => '<my email here>',
    pass => '<my password here>',
   }
);

open($out, ">",  "output_page.html") or die "Can't open output_page.html: $!";
print $out $mech->content;    

In the script I use Agent: Firefox but the cookies are exported from Chrome, is this maybe an issue? I have tryed different methods and languages Python and perl.

Jesse kraal
  • 323
  • 4
  • 11
  • http://stackoverflow.com/questions/31283839/facebook-login-via-perl-wwwmechanize-login-error-cookies-required – nexoma Jul 12 '15 at 18:21
  • Yes I have already read that topic, I tryd the solution by exporting the cookies.txt. unfortunately I recieve the same error: Cookies are not enabled on your browser. – Jesse kraal Jul 12 '15 at 19:11

2 Answers2

2

Always use strict and use warnings at the top of your script

In your script This line is problem

my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies::Netscape->new( file => $cookies.txt ) );

Change it to

my $cookies= 'cookies.txt'; #your cookies file path here
my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies::Netscape->new( file => $cookies ) );

Correcting all errors your script should be:

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies::Netscape; #you were using two times use
my $cookies='your cookies file path';
my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies::Netscape->new( file => $cookies) );
$mech->agent_alias('Windows Mozilla'); #this is right user agent
$mech->get("https://www.facebook.com/login.php");
$mech->submit_form(
fields => {
    email =>'your username',
    pass => 'your password',
   }
);

open(my $out, ">",  "output_page.html") or die "Can't open output_page.html: $!"; #make lexical variables using my
print $out $mech->content;

I will suggest you to read WWW::Mechanize documentation

Note: As many other users suggested this type of login using tools are against their ToS.So please check Facebook API.

Arunesh Singh
  • 3,489
  • 18
  • 26
1

There is no need to send credentials if you load correct cookies because this code works for me (as you can see you only need two cookies c_user and xs):

my $config = {

    auth_token =>
      "c_user=100009668980203;xs=143%3AfaO_5XiRk_rWOg%3A2%3A1435225463%3A-1",
};

my @cookies = split /;/, $config->{auth_token};

my $mech = WWW::Mechanize->new();

foreach my $cookie (@cookies) {

    my ( $cookie_name, $cookie_value ) = split /=/, $cookie, 2;
    $mech->cookie_jar->set_cookie( 0, $cookie_name, $cookie_value, '/',
        '.facebook.com', undef, 1, 1, undef, 1 );
}

$mech->get("https://www.facebook.com/messages/");

$mech->save_content("$Bin/Messages_test.htm", binmode => ':raw' );
gangabass
  • 10,607
  • 2
  • 23
  • 35