The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Help Connecting to Facebook
Discuss Help Connecting to Facebook in the Perl Programming forum on Dev Shed. Help Connecting to Facebook Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 1st, 2012, 07:35 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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&refsrc=http%3A%2F%2Fm.facebook.com%2Fhome.php&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>
|

November 1st, 2012, 07:38 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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.
|

November 1st, 2012, 10:20 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 15
Time spent in forums: 5 h 49 m 50 sec
Reputation 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();
|

November 1st, 2012, 05:41 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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.
|

November 1st, 2012, 05:51 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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?
|

November 1st, 2012, 05:54 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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.
|

November 2nd, 2012, 10:10 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 15
Time spent in forums: 5 h 49 m 50 sec
Reputation 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.
|

November 2nd, 2012, 11:45 AM
|
|
|
Quote:
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
|

November 3rd, 2012, 06:08 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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!
|

November 3rd, 2012, 06:10 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation 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?
|

November 5th, 2012, 12:17 PM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
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
Deta vil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE
|

November 6th, 2012, 01:31 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 15
Time spent in forums: 5 h 49 m 50 sec
Reputation 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
|

November 8th, 2012, 05:23 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 27

Time spent in forums: 4 h 25 m 15 sec
Reputation Power: 0
|
|
|
Ok thanks!
Also my email is leathan7@gmail.com... not hespineli@gmail.com
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|