Saturday, April 20, 2013

Login into your site using facebook account

Step 1:

The primary step is to visit http://developers.facebook.com/apps.
If you are visiting this URL for the first time, you will have a window similar to the one shown below in which Facebook developer app will request your permission to access your basic information.

Click the ‘Allow’ button.

Step 2:

Now you be on a page that shows you your recently viewed app. Don’t worry if it doesn’t show any app. It just means that you haven’t created any app yet.
Now, click ‘Create New App’ button.


In the window that pops up, enter the name of your application. Mark the check box ‘I agree to Facebook Terms’ and click ‘Continue’ button.
Now you may be asked to solve a captcha which verifies that you are human.

Step 3:

You will be taken to the application basic settings page.
On the top portion of the page you will have your ‘App ID’ and ‘App Secret’.
Enter your domain name as ‘App Domain’. Please note that a ‘your-domain.com’ will include ‘*.your-domain.com’ also. That is, you can use the app on any of your sub-domains. You can even enter ‘localhost’ if you are testing your application on your local machine. If localhost not working you need to setup your virtual host in case you use apache as server.
In the ‘Website with Facebook Login’ section, you need to enter ‘Site URL’. ‘Site URL’ will be entry page for your application. As we are making a webpage that has f-connect enabled, we need to give the address of the webpage that we are about to make.Because of security reasons, Facebook will redirect users to this URL only. We will be creating webpage in the later sections. If you are not sure about what your URL will be, just leave the field blank as you can fill it any time later.











Step 4:
Now write your php application. Suppose file index.php.
Define your scopes to want from user:
https://developers.facebook.com/docs/reference/login/#permissions
email, read_stream, user_birthday, user_location, user_work_history, 
user_hometown, user_photos



<?php
session_start();
$app_id = "250395365104464";
$app_secret = "3b616d9db3dc586b5ed784ef89e5f1a5";
$my_url = "http://thecontactspace.com/site/fb";
$scope = "email";
if(isset($_GET["logout"])) {
    unset($_SESSION["__access_token"]);
}

if(isset($_SESSION["__access_token"])) {
    echo "<a href='?logout'>Logout</a>";
    $graph_url = "https://graph.facebook.com/me?" .  
        $_SESSION["__access_token"];
    $user = json_decode(file_get_contents($graph_url));
    print_r($user); 
    die();
} else {
    if(!isset( $_REQUEST["code"] ) ) {
        $dialog_url = "http://www.facebook.com/dialog/oauth?".
            "scope=$scope&client_id=" . 
            $app_id . "&redirect_uri=" .  
            "".rawurlencode($my_url);
        echo("<script> top.location.href='" . $dialog_url . "'</script>");
        return;
    } else {
        $code = $_REQUEST["code"];
        $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
        . $app_secret . "&code=" . $code;

        $access_token = file_get_contents($token_url);
        $_SESSION["__access_token"] = $access_token;
        header('Location: ' . $my_url);
    }
}
?>

No comments:

Post a Comment