#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0

    Help Connecting to Facebook


    Hi, Im currently in China, where facebook is blocked, I travel here once every other year or so. I wrote a script to connect to facebook in 2011 and put the script on my webserver so that I could use facebook while in China. However something has changed in the last year and my script is broken, any help getting it working again would be greatly appreciated. The following is the code that connects to facebook.


    <code>

    #!/usr/bin/perl
    use CGI qw(:standard);
    use LWP;
    use HTTP::Cookies;

    $myAgent = LWP::UserAgent->new;
    $myAgent->agent('Mozilla/4.76 [en] (Win98; U)');
    my @netscape_like_headers = (
    'Accept-Language' => 'en-US',
    'Accept-Charset' => 'iso-8859-1,*,utf-8',
    'Accept' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*",
    'Referer' => 'http://www.facebook.com/'

    );
    $myAgent->cookie_jar(
    HTTP::Cookies->new(
    file => 'mycookies.txt',
    autosave => 1
    )
    );
    $response = $myAgent->post( 'https://www.facebook.com/login.php?login_attempt=1',
    [
    'email' => 'myEmailHere',
    'pass' => 'myPasswordHere',
    ], @netscape_like_headers);

    $response = $myAgent->get('http://m.facebook.com/home.php', @netscape_like_headers);

    print header;
    print start_html("What I read...");
    $pageText = $response->content();
    print "$pageText";
    print end_html;

    </code>

    P.S. Please dont tell me to use a Facebook specific module, because my server has perl 5.10 and doesnt let me upload additional modules.. Also please dont post unless you actually have help for me.. (sorry if this makes me sound like a ****)

    P.S.S The following is some code I found online that was also last updated in 2011. (dont know why this would help... but maybe)

    <code>

    #!/usr/bin/perl
    use LWP;
    use HTTP::Cookies;
    use Term::ReadKey;
    use strict;

    # General vars
    my $login;
    my $password;
    my $status;
    my $auth_key;
    my $fb_dtsg;
    my $response;
    my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
    my @header = ( 'Referer' => 'http://www.facebook.com/',
    'User-Agent' => $user_agent);
    my $cookie_jar = HTTP::Cookies->new(
    file => 'cookies.dat',
    autosave => 1,
    ignore_discard => 1);
    my $browser = LWP::UserAgent->new;
    $browser->cookie_jar($cookie_jar);

    # Get login information & the status message to send.
    print "Facebook login name: ";
    $login = <>; chomp($login);

    print "Password: ";
    $password = <>; chomp($password);

    print "\nYour status (Facebook appears to have a 232 character limit): ";
    $status = <>; chomp($status);
    print "\nSending... ";
    # Login and get auth key
    #================================================

    $response = $browser->post('https://www.facebook.com/login.php?m=m&amp;refsrc=http%3A%2F%2Fm.facebook.com%2Fhome.php&amp;refid=8',
    ['email' => $login,
    'pass' => $password,
    'login' => 'Log In'], @header);
    $cookie_jar->extract_cookies( $response );
    $cookie_jar->save;
    $response = $browser->get('http://m.facebook.com/home.php', @header);

    $auth_key = $response->content;
    $auth_key =~ s/\n//g;
    $auth_key =~ s/^.*name="post_form_id" value="//;
    $auth_key =~ s/".*$//;

    $fb_dtsg = $response->content;
    $fb_dtsg =~ s/\n//g;
    $fb_dtsg =~ s/^.*name="fb_dtsg" value="//;
    $fb_dtsg =~ s/".*$//;
    # Submit the status update
    #================================================

    @header = ('Referer' => 'http://m.facebook.com/a/home.php',
    'User-Agent' => $user_agent,
    'Host' => 'm.facebook.com');

    $response = $browser->post('http://m.facebook.com/a/home.php?re974fcaf&refid=7&rbb94a931',
    ['fb_dtsg' => $fb_dtsg,
    'post_form_id' => $auth_key,
    'status' => $status,
    'update' => 'Share'], @header);

    # Did we do good here?
    if ($response->content eq '') {
    print "Done!\n\n";
    } else {
    print "An error occurred while setting your profile status.\n\n";
    $response->content;
    }
    # Now that we're done we can delete the cookies.dat file.
    exec('rm cookies.dat');

    </code>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Sorry for not using a nice code format for the previous code... Im connecting to this site through a proxy server, and for somereason the button wasnt working >.>

    Anyway im off to sleep, thanks in advance for any help, its greatly appreciated.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    It shows me as logged in when I tried this from windows cmd prompt. I will try it from my webserver.

    I had issues before when doing webscraping and I solved them with follow redirect on.

    Also, do you have WWW::Mechanize on your server? I found that module to be a finger-saver many times.

    Interesting, it does not work from my webserver but does work fine from command prompt. Possibly an IP problem?
    I tried it with WWW::Mechanize and it gave me a captcha image, then reported suspicious activity but it DID connect using Mechanize.

    Code:
    #!/usr/bin/perl
    BEGIN {
        my $b__dir = (-d '/home/my_DIR/perl'?'/home/_MY_DIR_/perl':( getpwuid($>) )[7].'/perl');
        unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC;
    }
    use WWW::Mechanize;
    
    $login = 'email';
    $password = 'passw0rt';
    print "Content-Type: text/html\n\n";
    $url = "http://www.facebook.com/home.php";
    $mech = WWW::Mechanize->new();
    $mech->cookie_jar(HTTP::Cookies->new());
    $mech->get($url);
    $mech->submit_form(
    	form_number => 1,
    	fields      => { email => $login,
       	             	 pass => $password },
    );
    die unless ($mech->success);
    $mech->click();
    push @{ $mech->requests_redirectable }, 'POST';
    print $mech->content();
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Thank you very mych for your reply, Im in a hurry atm and gtg but i will try this ASAP, thanks again man, please check back later.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Nevermind I had half an hour extra, I dont appear to have WWW:Mechanize which really... really sucks because i have used it before and its really helpful

    Also not to sound noobish but what does this do:
    BEGIN {
    my $b__dir = (-d '/home/my_DIR/perl'?'/home/_MY_DIR_/perl' getpwuid($>) )[7].'/perl');
    unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC;
    }

    Im guessing it locates WWW:Mechanize?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Error: Can't locate WWW/Mechanize.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ./fb4.pl line 2.
    BEGIN failed--compilation aborted at ./fb4.pl line 2.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    I certainly won't pretend to know exactly what it's saying but, without this:

    BEGIN {
    my $b__dir = (-d '/home/my_DIR/perl'?'/home/_MY_DIR_/perl' getpwuid($>) )[7].'/perl');
    unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC;
    }

    none of the added perl modules will work correctly. I didn't used to have this issue but every host I've been on in the last 3 years I've had to use that^.

    You can see where it says my_DIR and _MY_DIR_, those would both need to be changed in your case. Example is "PerlGrasshopper.com",
    my_DIR would instead be something like perlgrasshopper, or whatever your directory is.

    You can find your directory using $env.

    Also, if you have cpanel, you might be able to install WWW::Mechanize on your own. You don't have to upload it if they have cpanel configured right with the proper compilers and you have permission to do so. My server allowed me to do this.

    I can host a small facebook proxy script for you using www::mechanize on my server, if you'd like. The problems would be:

    1) Exactly what content are you trying to get from facebook?
    The script you posted wouldn't hold up a full proxy browser, it would only load the one page. Neither would the script I posted using www:mech. While it does successfully Auth you, it isn't making any more requests.

    2) Your login and password would be insecure. I wouldn't have to store them but you'd be passing them across a form. (I have no reason to log this info but that might be of concern too)

    3) I don't have the time to add much to it to make it semi-complete to follow links via proxy etc.

    4) The captcha doesn't submit (because there's no form action) and I eventually had to login and "okay" the suspicious activity to get it to work.

    Have you tried a full proxy perl script? I have used several of them in the past with great success. No modules were required other than lwp and it had a lot of options for disabling/enabling script, css, flash etc.

    If you can't use a full proxy perl script for some reason, have you considered using a VPN or even a remote desktop application to log into another computer and browse from there?

    And finally, download and use Tor from www.torproject.org - It's not fast usually but it'll get you proxied up pretty heavy.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Code:
    BEGIN {
    my $b__dir = (-d '/home/my_DIR/perl'?'/home/_MY_DIR_/perl' getpwuid($>) )[7].'/perl');
    unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC;
    }
    That's a very convoluted way to add a path the the @INC array.

    Use the lib pragma instead of that BEGIN block.

    http://search.cpan.org/~smueller/lib-0.63/lib_pm.PL
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Thanks again for the replies man, the VPN idea worked

    Take that China!

    I will still be investigating why my script fails and try to fix it, and thanks for offering to host a script.. Ok well im off (first to see if my server has cpanel) thanks again!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    If you for any reason need to use the VPN im signed up for just message me at hespineli@gmail.com

    Also: How do I check if my server has cpanel?
  20. #11
  21. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    http://mydomain.com/cpanel or /cp is the norm ...
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    For me, it is:
    http://www.domain.com:2082

    I have also had:
    http://www.domain.com:2083

    And I seen but didn't have:
    http://www.domain.com:2081
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    27
    Rep Power
    0
    Ok thanks!

    Also my email is leathan7@gmail.com... not hespineli@gmail.com

IMN logo majestic logo threadwatch logo seochat tools logo