RSYNC is really simple, you can do an RSYNC by a single command line on your console. so, probably now your thinking.., what's the difference of my script? the difference is that I was able to create a script that has configuration files for you to re-use it every time you have different directories and servers to SYNC on.
First, you have to know the basic command line to do RSYNC. please see below command line.
rsync -crtz --bwlimit=56 --stats -e "ssh -l [USERNAME] -p [PORT]" [SOURCE DIRECTORY] [DESTINATION IP]:[DESTINATION DIRECTORY]
[USERNAME] is your account username to the destination server. your username should be set in passwordless to make it run in background.
[PORT] is the port that you will be using to connect to destination server.
[SOURCE DIRECTORY] is the source directory of the files you want to SYNC on.
[DESTINATION IP] is the server ip of the destination server.
[DESTINATION DIRECTORY] is the destination directory of the files you want to SYNC on.
Once you know how the RSYNC works in background, next is to know how we want our configuration to work. we should have a script that reads configuration in a flat file.
In creating configuration file, you should consider the comment syntax. this is to easily comment the lines you wanted to skip on or disregard in the configuration file.
Usually in linux, commented lines are preceded by pound sign "#" and that should be one of your condition to disregard. please see below for the sample reading of file then check for pound sign "#" using REGEX, then disregard.
open FHFILE, "< $strConf";
@arrData = <FHFILE>;
close FHFILE;
foreach $strData (@arrData)
{
chomp $strData;
if ($strData !~ /^\s*\#/)
{
print "okay here";
}
}
The simple REGEX above checks if you have pound sign "#" on each lines and print okay if you don't.
The last thing you should know is how to pass the configuration file in command line. SYNTAX will be <YOUR SCRIPT> <space> <YOUR CONFIG FILE>. If you are new to LINUX, all parameters you passed on after your PERL SCRIPT will be catched by the ARRAY variable $ARGV. print that on your script with zero "0" as index will give you the first parameter you passed on, then succeeding index will the next parameter and so on and so forth.
Hope you were able to follow. Now we're ready to do the actual code.
Please see the recommended configuration FORMAT below that works on my RSYNC script. You will notice that I have a header which is setup to be the [DESTINATION IP] and [DESTINATION PORT]. This is for me to have it organize in a per SERVER configuration, then succeeding lines will be the actual SOURCE and DESTINATION directories.
- conf.txt as configuration file
[DESTINATION IP]:[DESTINATION PORT]
[SOURCE DIRECTORY 1]:[DESTINATION DIRECTORY 1]
[SOURCE DIRECTORY 2]:[DESTINATION DIRECTORY 2]
[SOURCE DIRECTORY n..]:[DESTINATION DIRECTORY n..]
- rsync.pl as your perl script
#!/usr/bin/perl
### reading conf files
$strConf = $ARGV[0];
if (-e $strConf)
{
&do_rsync($strConf);
}
else
{
print "conf: $strConf does not exist!!\n";
}
###
sub do_rsync
{
my ($strConf) = @_;
my ($ip, $port, $cmd, $strData, $strIncomingDir, $strOutGoingDir, $strReturn);
my @arrData;
open FHFILE, "< $strConf";
@arrData = <FHFILE>;
close FHFILE;
$ip_port = shift @arrData;
chomp $ip_port;
# reading the ip and port as header
while ($ip_port =~ /^\s*\#/)
{
$ip_port = shift @arrData;
chomp $ip_port;
}
# end
($ip, $port) = split /\:/, $ip_port;
if ($ip =~ /[^0-9|.]/)
{
print "ip not valid..\n\n";
}
elsif ($port =~ /[^0-9]/)
{
print "port not valid..\n\n";
}
else
{
# reading each line after the header
foreach $strData (@arrData)
{
chomp $strData;
if ($strData !~ /^\s*\#/)
{
($strIncomingDir, $strOutGoingDir) = split /\:/, $strData;
$cmd = "rsync -crtz --bwlimit=56 --stats -e \"ssh -l contents -p $port\" $strIncomingDir $ip:$strOutGoingDir";
$strReturn = `$cmd`;
}
}
# end
}
}
1;
To execute, please see command line below. you can also put this in CRON, provided that the USERNAME is set to be PASSWORDLESS.
<path to the script>/rsync.pl <path to the config file>/conf.txt
Hope you like it!! Please leave a comment if you find this helpful. Thanks!!
You should know your purpose in life, make a difference, and believe in yourself. Always think positive, take the risk and move forward. Do that, then before you know it, you're already the man you always wanted. Cheers!!
Tuesday, January 25, 2011
How to create an RSYNC script with configuration in PERL
Tuesday, January 18, 2011
How to post facebook status using OAuth with permanent TOKEN via backend in PERL and PHP
This post will teach you how to post facebook status using OAuth process via back-end. This is actually a revised version of the script I made on my previous post How to submit facebook status via back-end using PERL but instead of saving the users credentials, you just need to save the token which we get from the OAuth process.
You just need to have a web server coz facebook OAuth will require application to have a site URL as to be the callback URL in passing information. just for the sake of our testing we can use our local domain.
Just follow the simple steps below and we will be able to submit facebook status via back-end using OAuth process the "LEGAL WAY".
1. Same with my previous post, you should register your application on facebook - http://www.facebook.com/developers/createapp.php again, type in the application name and other details then once created, modify the site URL under web site tab.
2. Once you have the 3 details such as: app ID, app Secret, and site URL. we can now start coding the authorization script which will request permission for our app to do the status update.
app ID - 182635521758593
app Secret - 495625ad928ea277548d0f423f420ef0
site URL - http://localhost/facebook/
3. Since we're using PERL, you have to install the following libraries needed to run the script.
5. Login to the wap site via the URL - http://m.facebook.com/login.php then save the cookies.
6. Request for permission to post facebook status with the app ID, and site URL of your application where app ID being the "app_id" parameter and site URL being the "next" parameter. just for this testing, we can use the application "Hotshots Point Of View". the application details are stated on step no. 2.
7. Get the $response->content and parse the "form action", "post_form_id", and "fb_dtsg" via REGEX implementation below. take note that this might change as the facebook wap changes. take note as well that the $response->content might not return as expected if the user already allow the application. hence, the $response->content will be the return output of your callback or site URL. if this is the first time that the user will allow the application, expect a return page with the details we need below.
8. Once we have the "form action", "post_form_id", and "fb_dtsg", we can now trigger user to allow our application. please see below with other details we have from step no. 2, then clear the cookies by unlink() function.
9. Okay, we are just halfway there.. now that we are able to allow the app to update facebook status, next step will be the script to post facebook status, but before that, here is the complete code of the PERL script as detailed on the steps above.
10. Succeeding steps will then teach you how to submit facebook status in PHP which was triggered by facebook upon allowing our application. if you notice on our authorize URL on step no. 6, we set the "next" parameter to be the same as the value of our facebook app site URL. the "next" parameter will be used by facebook to return the CODE which we can exchange for a TOKEN that we will be using to post facebook status. please see below authorize URL from step no. 6.
http://m.facebook.com/connect/uiserver.php?app_id=182635521758593&method=permissions.request&display=wap&next=http%3A%2F%2Flocalhost%2Ffacebook%2F&response_type=code&fbconnect=1&perms=user_photos%2Cuser_videos%2Cpublish_stream
11. In back-end, the URL below was executed by our PERL script but if this link was clicked by the user, the user will be redirected to the page where in our facebook application is requesting for permission to post facebook status on users profile. if the user will allow it, facebook will then redirect it to the "next" parameter we specify on the URL above. please see facebook redirection URL format below.
http://localhost/facebook/?code=...
12. Your index page should be able to capture the CODE parameter returned by facebook and exchange it with TOKEN on the access token URL below then parse the return data to get the TOKEN. again, app ID will be the "client_id" parameter, site URL will the "redirect_uri" parameter, and the app Secret will be the "client_secret" parameter.
13. Once you have the TOKEN, you will now be able to post facebook status using CURL. please see below implementation.
14. Please take note that the TOKEN you just pulled from facebook is NOT yet permanent. Hence, you need to call another access token with the parameter grant_type=client_credentials.
15. Please see below for the complete PHP script.
Save the USERID and TOKEN for succeeding facebook status. Hope you like it!! Enjoy!! yeah men!! - http://paulgonzaga.blogspot.com
You just need to have a web server coz facebook OAuth will require application to have a site URL as to be the callback URL in passing information. just for the sake of our testing we can use our local domain.
Just follow the simple steps below and we will be able to submit facebook status via back-end using OAuth process the "LEGAL WAY".
1. Same with my previous post, you should register your application on facebook - http://www.facebook.com/developers/createapp.php again, type in the application name and other details then once created, modify the site URL under web site tab.
2. Once you have the 3 details such as: app ID, app Secret, and site URL. we can now start coding the authorization script which will request permission for our app to do the status update.
app ID - 182635521758593
app Secret - 495625ad928ea277548d0f423f420ef0
site URL - http://localhost/facebook/
3. Since we're using PERL, you have to install the following libraries needed to run the script.
- LWP::UserAgent;
- HTTP::Cookies;
#!/usr/bin/perl
require LWP::UserAgent;
use strict;
use warnings;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new;
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://m.facebook.com/','User-Agent' => $user_agent);
my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
file => $cookie_file,
autosave => 1,
ignore_discard => 1);
$lwpua->cookie_jar($cookie_jar);
5. Login to the wap site via the URL - http://m.facebook.com/login.php then save the cookies.
my $strUser = '<your facebook username/email>';
my $strPass = '<your facebook password>';
my $strStatus = '<your facebook status>';
# login to facebook
my $response = $lwpua->post('http://m.facebook.com/login.php',
['email' => $strUser,
'pass' => $strPass,
'login' => 'Login'], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
6. Request for permission to post facebook status with the app ID, and site URL of your application where app ID being the "app_id" parameter and site URL being the "next" parameter. just for this testing, we can use the application "Hotshots Point Of View". the application details are stated on step no. 2.
$response = $lwpua->get('http://m.facebook.com/connect/uiserver.php?app_id=182635521758593&method=permissions.request&display=wap&next=http%3A%2F%2Flocalhost%2Ffacebook%2F&response_type=code&fbconnect=1&perms=user_photos%2Cuser_videos%2Cpublish_stream', @header);
7. Get the $response->content and parse the "form action", "post_form_id", and "fb_dtsg" via REGEX implementation below. take note that this might change as the facebook wap changes. take note as well that the $response->content might not return as expected if the user already allow the application. hence, the $response->content will be the return output of your callback or site URL. if this is the first time that the user will allow the application, expect a return page with the details we need below.
my $form_data = $response->content;
$form_data =~ s/\n//g;
$form_data =~ /form id="uiserver_form" action="(.*?)"(.*?)name="post_form_id" value="(.*?)"(.*?)name="fb_dtsg" value="(.*?)"/ig;
my $form_action = $1;
my $form_id = $3;
my $form_fbdtsg = $5;
8. Once we have the "form action", "post_form_id", and "fb_dtsg", we can now trigger user to allow our application. please see below with other details we have from step no. 2, then clear the cookies by unlink() function.
$response = $lwpua->post('http://m.facebook.com/connect/uiserver.php',
['fb_dtsg' => $form_fbdtsg,
'post_form_id' => $form_id,
'app_id' => '182635521758593',
'display' => 'wap',
'redirect_uri' => 'http://localhost/facebook/',
'response_type' => 'code',
'fbconnect' => '1',
'perms' => 'user_photos,user_videos,publish_stream',
'from_post' => '1',
'__uiserv_method' => 'permissions.request',
'grant_clicked' => 'Allow'], @header);
$form_data = $response->content;
unlink($cookie_file);
9. Okay, we are just halfway there.. now that we are able to allow the app to update facebook status, next step will be the script to post facebook status, but before that, here is the complete code of the PERL script as detailed on the steps above.
#!/usr/bin/perl
require LWP::UserAgent;
use strict;
use warnings;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new;
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://m.facebook.com/','User-Agent' => $user_agent);
my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
file => $cookie_file,
autosave => 1,
ignore_discard => 1);
$lwpua->cookie_jar($cookie_jar);
my $strUser = '<your facebook username/email>';
my $strPass = '<your facebook password>';
# login to facebook
my $response = $lwpua->post('http://m.facebook.com/login.php',
['email' => $strUser,
'pass' => $strPass,
'login' => 'Login'], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $lwpua->get('http://m.facebook.com/connect/uiserver.php?app_id=182635521758593&method=permissions.request&display=wap&next=http%3A%2F%2Flocalhost%2Ffacebook%2F&response_type=code&fbconnect=1&perms=user_photos%2Cuser_videos%2Cpublish_stream', @header);
my $form_data = $response->content;
$form_data =~ s/\n//g;
$form_data =~ /form id="uiserver_form" action="(.*?)"(.*?)name="post_form_id" value="(.*?)"(.*?)name="fb_dtsg" value="(.*?)"/ig;
my $form_action = $1;
my $form_id = $3;
my $form_fbdtsg = $5;
$response = $lwpua->post('http://m.facebook.com/connect/uiserver.php',
['fb_dtsg' => $form_fbdtsg,
'post_form_id' => $form_id,
'app_id' => '182635521758593',
'display' => 'wap',
'redirect_uri' => 'http://localhost/facebook/',
'response_type' => 'code',
'fbconnect' => '1',
'perms' => 'user_photos,user_videos,publish_stream',
'from_post' => '1',
'__uiserv_method' => 'permissions.request',
'grant_clicked' => 'Allow'], @header);
$form_data = $response->content;
unlink($cookie_file);
1;
10. Succeeding steps will then teach you how to submit facebook status in PHP which was triggered by facebook upon allowing our application. if you notice on our authorize URL on step no. 6, we set the "next" parameter to be the same as the value of our facebook app site URL. the "next" parameter will be used by facebook to return the CODE which we can exchange for a TOKEN that we will be using to post facebook status. please see below authorize URL from step no. 6.
http://m.facebook.com/connect/uiserver.php?app_id=182635521758593&method=permissions.request&display=wap&next=http%3A%2F%2Flocalhost%2Ffacebook%2F&response_type=code&fbconnect=1&perms=user_photos%2Cuser_videos%2Cpublish_stream
11. In back-end, the URL below was executed by our PERL script but if this link was clicked by the user, the user will be redirected to the page where in our facebook application is requesting for permission to post facebook status on users profile. if the user will allow it, facebook will then redirect it to the "next" parameter we specify on the URL above. please see facebook redirection URL format below.
http://localhost/facebook/?code=...
12. Your index page should be able to capture the CODE parameter returned by facebook and exchange it with TOKEN on the access token URL below then parse the return data to get the TOKEN. again, app ID will be the "client_id" parameter, site URL will the "redirect_uri" parameter, and the app Secret will be the "client_secret" parameter.
$code = $_GET['code'];
$oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=$code";
$url_handler = fopen("$oauthurl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$ret = explode("&", $url_contents);
$token = preg_replace('/^access_token=/', '', $ret[0]);
13. Once you have the TOKEN, you will now be able to post facebook status using CURL. please see below implementation.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POSTFIELDS,'access_token='.urlencode($token).'&message='.urlencode($status));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://m.facebook.com");
$page = curl_exec($ch);
14. Please take note that the TOKEN you just pulled from facebook is NOT yet permanent. Hence, you need to call another access token with the parameter grant_type=client_credentials.
$oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=182635521758593&client_secret=495625ad928ea277548d0f423f420ef0&grant_type=client_credentials";
$url_handler = fopen("$oauthurl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$ret = explode("&", $url_contents);
$token = preg_replace('/^access_token=/', '', $ret[0]);
15. Please see below for the complete PHP script.
<?
$status = "damn!! i'm good!! i was able to crack facebook oauth process via backend using perl and php! - http://paulgonzaga.blogspot.com";
$code = $_GET['code'];
if ($code) {
// get access token
$oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=$code";
$url_handler = fopen("$oauthurl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$ret = explode("&", $url_contents);
$token = preg_replace('/^access_token=/', '', $ret[0]);
if ($token) {
// get user info
$infourl = "https://graph.facebook.com/me?access_token=$token";
$url_handler = fopen("$infourl", 'r');
$return = json_decode(stream_get_contents($url_handler));
fclose($url_handler);
$userid = $return->id;
$name = $return->name;
$fname = $return->first_name;
$mname = $return->middle_name;
$lname = $return->last_name;
$oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=182635521758593&client_secret=495625ad928ea277548d0f423f420ef0&grant_type=client_credentials";
$url_handler = fopen("$oauthurl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$ret = explode("&", $url_contents);
$token = preg_replace('/^access_token=/', '', $ret[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/$userid/feed');
curl_setopt($ch, CURLOPT_POSTFIELDS,'access_token='.urlencode($token).'&message='.urlencode($status));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://m.facebook.com");
$page = curl_exec($ch);
}
}
?>
Save the USERID and TOKEN for succeeding facebook status. Hope you like it!! Enjoy!! yeah men!! - http://paulgonzaga.blogspot.com
Sunday, January 16, 2011
The TOP 15 most important social networking sites.
The following list are the most important social networking sites as of today. The list are not based on the popularity of the sites nor how old the website is running. This is based on its importance to people's lives, self improvement, business, friendship, and how we live our lives.
Ryze, Bebo, BlackPlanet.com, Flickr.com, Reunion.com, aSmallWorld, Bebo, BlackPlanet.com, Blue Dot, Bolt, Broadcaster.com, Buzznet, CarDomain, Consumating, Couchsurfing, Cyworld, Dandelife, DeadJournal, DontStayIn, Doostang, Ecademy, eSPIN, Faceparty, Flirtomatic, Fotki, Friends Reunited, Gaia Online, Geni.com, GoPets, Graduates.com, Grono.net, Hyves, imeem, Infield Parking, IRC-Galleria, iWiW, Joga, Bonito, Last.fm, LibraryThing, LiveJournal, LunarStorm, MEETin, MiGente.com, Mixi, MOG, Multiply, My Opera Community, myYearbook, Netlog, Nexopia, OUTeverywhere, Passado, Piczo, Playahead, ProfileHeaven, Pownce, RateItAll, Searchles, Sconex, Shelfari, Soundpedia, Sportsvite, Studivz, TagWorld, TakingITGlobal, The Doll Palace, The Student Center, Threadless, TravBuddy.com, Travellerspoint, Tribe.net, Vampire Freaks, Vox, WAYN, WebBiographies, Windows Live Spaces, Woophy, XING, Xuqa, Yelp, Zaadz, Zooomr
- Facebook - Initially intended for college students, then branched out by invite, and continuously growing with over 500 million members.
- Twitter - A free social networking service that allows users to send "updates" (text-based posts that are up to 140 characters long) via SMS, instant messaging, email, the Twitter website, or an application such as Twitterrific. The site has become very popular in only a few months with over 75 million members.
- MySpace - Over 130 million members. this site is massive, boasting the largest membership of any social networking site on the internet.
- Linkedin - Over 75 million members. a powerful for business networking. mainly used by businessmen and organizations.
- Foursquare - Foursquare is a social networking site build by Google that shares your global location by checking in, sending shout out, and even promote the location by leaving tips.
- Friendster - It was considered the top online social networking service until it was overtaken by MySpace at around April 2004. Demographic studies shows that users are between 17 to 30 years of age and over 90 million members.
- StumbleUpon - Boasting over 13 million users, StumbleUpon is a web browser plugin that allows its users to discover and rate web pages, photos, videos, and news article. We usually see this plugin on social bookmarking tools like addThis, shareThis, etc.. A great way to promote websites. It was bought by eBay for $75 million in May 2007.
- Delicious - The website delicious which is formerly known as "del.icio.us" is a social bookmarking web service for storing, sharing, and discovering web bookmarks. The site was founded by Joshua Schachter in late 2003, and is now part of Yahoo!
- Digg - Digg is a website made for people to share and discover content on the internet by submitting links and stories, then people votes and comments on submitted contents in a social and democratic spirit.
- Orkut - Orkut is an Internet social networking service run by Google and named after its creator, Google employee Orkut Büyükkökten. It claims to be designed to help users meet new friends and maintain existing relationships. Now has a membership of 100 million.
- Classmates - Over 50 million members. One of the oldest social networking sites around, Classmates was kicked off in 1995, and has proven to be a great way for members to to connect with old friends and acquaintances from throughout their lives.
- Meetup - Over 2 million members. Meetup.com is an online social networking portal that facilitates offline group meetings in various localities around the world. Meetup allows members to find and join groups unified by a common interest, such as politics, books, games, movies, health, pets, careers or hobbies.
- Yahoo! Pulse - Formerly known as Yahoo! 360° (a.k.a Yahoo! Days) is a personal communication portal similar to Orkut and MySpace -- it is now launched for public used already. It integrates features of social networking, blogging and photo sharing sites.
- Xanga - Over 27 million members. Xanga the blogging community is a free Web-based service that hosts weblogs, photoblogs, videoblogs, audioblogs, and social networking profiles.
- Care2 - Over 14 million members. Care2 is a social networking website that was founded to help connect activists from around the world.
Ryze, Bebo, BlackPlanet.com, Flickr.com, Reunion.com, aSmallWorld, Bebo, BlackPlanet.com, Blue Dot, Bolt, Broadcaster.com, Buzznet, CarDomain, Consumating, Couchsurfing, Cyworld, Dandelife, DeadJournal, DontStayIn, Doostang, Ecademy, eSPIN, Faceparty, Flirtomatic, Fotki, Friends Reunited, Gaia Online, Geni.com, GoPets, Graduates.com, Grono.net, Hyves, imeem, Infield Parking, IRC-Galleria, iWiW, Joga, Bonito, Last.fm, LibraryThing, LiveJournal, LunarStorm, MEETin, MiGente.com, Mixi, MOG, Multiply, My Opera Community, myYearbook, Netlog, Nexopia, OUTeverywhere, Passado, Piczo, Playahead, ProfileHeaven, Pownce, RateItAll, Searchles, Sconex, Shelfari, Soundpedia, Sportsvite, Studivz, TagWorld, TakingITGlobal, The Doll Palace, The Student Center, Threadless, TravBuddy.com, Travellerspoint, Tribe.net, Vampire Freaks, Vox, WAYN, WebBiographies, Windows Live Spaces, Woophy, XING, Xuqa, Yelp, Zaadz, Zooomr
Saturday, January 15, 2011
How to submit facebook status via OAuth.
You should read my last 2 previous post before this to know how the OAuth process works.
This post will teach you how to submit facebook status using OAuth. just follow the simple steps below with the complete code at the end for your implementation.
1. Same with my previous post, you need to have an app ID, site URL, and app Secret to submit facebook status on users profile. just for this testing, you can use the details below from the facebook app "Hotshots Point Of View".
app ID - 182635521758593
site URL - http://localhost/facebook/
app Secret - 495625ad928ea277548d0f423f420ef0
2. After having the 3 details, you should have to request permission for your app to be able to access users profile, but this time an added permission to post status. to do that, an additional parameter SCOPE=user_photos,user_videos,publish_stream should be added on your authorize URL. again, app ID will be your client_id, and site URL will be your redirect_uri. please see below authorize URL with the permission to post status on users profile.
https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/&scope=user_photos,user_videos,publish_stream
3. User should click the URL above and authorize your application to access profile and submit status. facebook will return to the redirect_uri http://localhost/facebook/ with the parameter CODE as part of query string. capture the CODE parameter and exchange it for an access TOKEN for the next step below.
4. To get an access token, you should pass the app ID, site URL, app Secret, and the CODE parameter value you get from the authorize URL. please see the access token URL below. you should replace the <CODE> with the CODE parameter you get from the authorize URL above.
https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<CODE>
5. From the URL above, facebook will echo the access TOKEN which you will need to post facebook status. you will need to get the user id of the user you wish to post facebook status, and to do that, just replace the <TOKEN> with the access TOKEN on the URL below.
https://graph.facebook.com/me?access_token=<TOKEN>
6. Once you have the USER ID, and TOKEN, you can now finally post facebook status on users profile. execute the CURL script below with your facebook STATUS.
For you to have a working script in submitting facebook status. please see below script that does all in 1 page: authorize app, access token, get user info, and post facebook status.
Hope you like it!! happy coding.
This post will teach you how to submit facebook status using OAuth. just follow the simple steps below with the complete code at the end for your implementation.
1. Same with my previous post, you need to have an app ID, site URL, and app Secret to submit facebook status on users profile. just for this testing, you can use the details below from the facebook app "Hotshots Point Of View".
app ID - 182635521758593
site URL - http://localhost/facebook/
app Secret - 495625ad928ea277548d0f423f420ef0
2. After having the 3 details, you should have to request permission for your app to be able to access users profile, but this time an added permission to post status. to do that, an additional parameter SCOPE=user_photos,user_videos,publish_stream should be added on your authorize URL. again, app ID will be your client_id, and site URL will be your redirect_uri. please see below authorize URL with the permission to post status on users profile.
https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/&scope=user_photos,user_videos,publish_stream
3. User should click the URL above and authorize your application to access profile and submit status. facebook will return to the redirect_uri http://localhost/facebook/ with the parameter CODE as part of query string. capture the CODE parameter and exchange it for an access TOKEN for the next step below.
4. To get an access token, you should pass the app ID, site URL, app Secret, and the CODE parameter value you get from the authorize URL. please see the access token URL below. you should replace the <CODE> with the CODE parameter you get from the authorize URL above.
https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<CODE>
5. From the URL above, facebook will echo the access TOKEN which you will need to post facebook status. you will need to get the user id of the user you wish to post facebook status, and to do that, just replace the <TOKEN> with the access TOKEN on the URL below.
https://graph.facebook.com/me?access_token=<TOKEN>
6. Once you have the USER ID, and TOKEN, you can now finally post facebook status on users profile. execute the CURL script below with your facebook STATUS.
curl -F 'access_token=<TOKEN>' -F 'message=<STATUS>' https://graph.facebook.com/<USERID>/feed
For you to have a working script in submitting facebook status. please see below script that does all in 1 page: authorize app, access token, get user info, and post facebook status.
<?
$client_id = '182635521758593';
$redirect_uri = 'http://localhost/facebook/';
$client_secret = '495625ad928ea277548d0f423f420ef0';
$status = 'How to submit facebook status via OAuth - http://paulgonzaga.blogspot.com/2011/01/how-to-submit-facebook-status-via-oauth.html';
$code = $_GET['code'];
if ($code) {
// get access token
$oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code";
$data = do_get_request($oauthurl);
$ret = explode("&", $data);
$token = preg_replace('/^access_token=/', '', $ret[0]);
// get user info
$infourl = "https://graph.facebook.com/me?access_token=$token";
$data = do_get_request($infourl);
$ret = json_decode($data);
$userid = $ret->id;
$result = shell_exec("curl -F 'access_token=$token' -F 'message=$status' https://graph.facebook.com/$userid/feed");
echo "done!</br>";
}
function do_get_request($get_url)
{
$url_handler = fopen("$get_url", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
return $url_contents;
}
?>
<html>
<head>
</head>
<body>
<a href="https://graph.facebook.com/oauth/authorize?client_id=<?=$client_id?>&redirect_uri=<?=$redirect_uri?>&scope=user_photos,user_videos,publish_stream">authorize app</a>
</body>
</html>
Hope you like it!! happy coding.
How to use facebook application into your site.
I assume you know already how to create a facebook application from my previous post, coz on this post, I will tell you how to use it in your own website.
We will be using the OAuth process which is the legal way in posting facebook status.
Just follow the simple steps below and we can have our simple facebook app working in our own website.
1. first, you have to get your facebook app ID from - http://www.facebook.com/developers/apps.php
2. register the URL of your site by clicking the "Edit Settings" link. go to Web Site Settings by clicking the "Web Site" tab, then type in the URL of your site. just for testing, we can always use our local domain, coz facebook allows it. ex. http://localhost/facebook/
3. after registering our site URL, we can now create a simple app that will request to allow our facebook application to access profile and submit status.
4. just for testing purposes, I am allowing everyone to use my own created facebook application "Hotshots Point Of View". this application is configured to use your own local domain. please see details below.
app ID - 182635521758593
app Secret - 495625ad928ea277548d0f423f420ef0
site URL - http://localhost/facebook/
5. if you will use my facebook app, you must create a "facebook" directory under your localhost domain. copy the code below and save it as index.php. on the URL below, app ID will be your client_id and the site URL will be your redirect_uri.
<html>
<head>
</head>
<body>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
</body>
</html>
6. this is just a simple interface with a link labeled as "authorize app". go to your created app - http://localhost/facebook/ then click the "authorize app" link. you should be redirected to facebook page requesting permission to allow "Hotshots Point Of View" to access your basic information, if you are not currently logged-in, you will be prompted to log-in.
7. If the user authorizes your application, facebook will redirect the user back to the redirect URI we specified with a verification string in the parameter
8. to capture the value of the CODE parameter, we will modify our index.php as below. use the CODE to access token URL. pass the same client_id and redirect_uri as in the previous step together with the CODE and App Secret as stated above. this will now be the content of our index.php.
<?
$code = $_GET['code'];
?>
<html>
<head>
</head>
<body>
<? if (!$code) : ?>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
<? else : ?>
<a href="https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<?=$code?>">access token</a>
<? endif; ?>
</body>
</html>
9. if CODE parameter doesn't have value, the link will be labeled as "authorize app" and if it has, the link will be labeled as "access token". try accessing your app again - http://localhost/facebook/
10. after clicking the "access token", it will goes back again to your site and echo the access_token which you will be use to access users profile.
11. save the access_token returned by facebook then use it to access profile. please see simple code below in PHP to get the users profile.
<?
$token = '<access token>';
// get user info
$infourl = "https://graph.facebook.com/me?access_token=$token";
$url_handler = fopen("$infourl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$return = json_decode($url_contents);
$userid = $return->id;
$fname = $return->first_name;
$mname = $return->middle_name;
$lname = $return->last_name;
echo "$fname $mnane $lname\n";
?>
12. now, that you were able to get the users profile, you can have that integrated within your site.
Hope you were able to follow the steps I made. submission of facebook status will NOT be able work on this post coz we have to specify a SCOPE to authorize our application to publish stream but don't you worry coz that will be next. Enjoy!! yeah men!!
We will be using the OAuth process which is the legal way in posting facebook status.
Just follow the simple steps below and we can have our simple facebook app working in our own website.
1. first, you have to get your facebook app ID from - http://www.facebook.com/developers/apps.php
2. register the URL of your site by clicking the "Edit Settings" link. go to Web Site Settings by clicking the "Web Site" tab, then type in the URL of your site. just for testing, we can always use our local domain, coz facebook allows it. ex. http://localhost/facebook/
3. after registering our site URL, we can now create a simple app that will request to allow our facebook application to access profile and submit status.
4. just for testing purposes, I am allowing everyone to use my own created facebook application "Hotshots Point Of View". this application is configured to use your own local domain. please see details below.
app ID - 182635521758593
app Secret - 495625ad928ea277548d0f423f420ef0
site URL - http://localhost/facebook/
5. if you will use my facebook app, you must create a "facebook" directory under your localhost domain. copy the code below and save it as index.php. on the URL below, app ID will be your client_id and the site URL will be your redirect_uri.
<html>
<head>
</head>
<body>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
</body>
</html>
6. this is just a simple interface with a link labeled as "authorize app". go to your created app - http://localhost/facebook/ then click the "authorize app" link. you should be redirected to facebook page requesting permission to allow "Hotshots Point Of View" to access your basic information, if you are not currently logged-in, you will be prompted to log-in.
7. If the user authorizes your application, facebook will redirect the user back to the redirect URI we specified with a verification string in the parameter
CODE - http://localhost/facebook/?code=.... the CODE parameter can be exchanged for an OAuth access token for us to be able to access users profile.8. to capture the value of the CODE parameter, we will modify our index.php as below. use the CODE to access token URL. pass the same client_id and redirect_uri as in the previous step together with the CODE and App Secret as stated above. this will now be the content of our index.php.
<?
$code = $_GET['code'];
?>
<html>
<head>
</head>
<body>
<? if (!$code) : ?>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
<? else : ?>
<a href="https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<?=$code?>">access token</a>
<? endif; ?>
</body>
</html>
9. if CODE parameter doesn't have value, the link will be labeled as "authorize app" and if it has, the link will be labeled as "access token". try accessing your app again - http://localhost/facebook/
10. after clicking the "access token", it will goes back again to your site and echo the access_token which you will be use to access users profile.
11. save the access_token returned by facebook then use it to access profile. please see simple code below in PHP to get the users profile.
<?
$token = '<access token>';
// get user info
$infourl = "https://graph.facebook.com/me?access_token=$token";
$url_handler = fopen("$infourl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);
$return = json_decode($url_contents);
$userid = $return->id;
$fname = $return->first_name;
$mname = $return->middle_name;
$lname = $return->last_name;
echo "$fname $mnane $lname\n";
?>
12. now, that you were able to get the users profile, you can have that integrated within your site.
Hope you were able to follow the steps I made. submission of facebook status will NOT be able work on this post coz we have to specify a SCOPE to authorize our application to publish stream but don't you worry coz that will be next. Enjoy!! yeah men!!
Subscribe to:
Posts (Atom)
Leadership 101
- Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
- Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
- Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
- Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
- Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
- Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.