Monday, February 25, 2013

Connect to facebook using localhost


Steps to connect to facebook from localhost

1. We need to trick facebook in believing we are from that we are from actual domain instead of localhost.

So what we need to do is create a host 

So go to /etc/hosts on ubuntu one can go in windows apache/hosts file and add the following

/etc/hosts

127.0.0.1       localhost
127.0.1.1       your local ip address or whatever
127.0.0.1       abo.mysite
127.0.1.1       abo.mysite

2. Now go to apache config
/etc/apache2/sites enabled/000-default and add the following

<VirtualHost abo.mysite:80>
 DocumentRoot /var/www/
    <Directory /var/www>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

 Now restart apache sudo /etc/init.d/apache2 restart and test the new locahost
http://abo.mysite this should resolve now.

3. No go to http://apps.facebook.com and create a new app after you create a app you should have settings as showin in the image below.






File index.php

require_once("library/facebook.php");

$facebook= new Facebook(array(
                'appId'  => '$appid',
                'secret' => '#appsecretkey'
            ));


$me = null;
// some random permissions
$permissions = 'publish_stream, create_event, rsvp_event, manage_pages, ads_management, user_photos';

// Session based API call.

    try {
        $uid = $facebook->getUser();
        echo $uid;
        $me = $facebook->api('/' . $uid);

    } catch (FacebookApiException $e) {
        var_dump($e);
    }

// here, check if the user has the application added
if ($me == null) {
    $loginUrl = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0,
        'req_perms' => $permissions,
        'next' => CALLBACK_URL . '?installed=1', // will be redirected to this URL after pressing "allow"
        'cancel_url' => CALLBACK_URL . '?cancel', // will be redirected here if app is not allowed
    )
    ); // end of array
    // redirect the user to the $loginUrl, you can place a button if you want
  
}
$user_profile = $facebook->api('/me');
var_dump($user_profile);   
exit;

2 comments:

  1. wonderful points altogether, you simply gained a new reader.
    What may you suggest about your post that you simply
    made a few days in the past? Any certain?

    Feel free to visit my webpage :: http://atlaslm.com

    ReplyDelete
  2. Howdy just wanted to give you a brief heads up and let you know a
    few of the images aren't loading correctly. I'm not sure why but
    I think its a linking issue. I've tried it in two different browsers and both show the same results.

    My website; sd765.sivit.Org

    ReplyDelete