hunter
hunter

Reputation: 1101

how to integrate my site to facebook

I want to integrate my site to the Facebook login system. I googled it and at last I find this great tutorial.

But there is a problem in my authentication. Here you can find my test site. I have used php-sdk; please help where I make a mistake.

<?php
    include_once "fbmain.php";
    $config['baseurl']  =   "http://www.cpantry.com/match/index.php";

    //if user is logged in and session is valid.
    if ($fbme){
        //Retriving movies those are user like using graph api
        try{
            $movies = $facebook->api('/me/movies');
        }
        catch(Exception $o){
            d($o);
        }

        //Calling users.getinfo legacy api call example
        try{
            $param  =   array(
                'method'  => 'users.getinfo',
                'uids'    => $fbme['id'],
                'fields'  => 'name,current_location,profile_url',
                'callback'=> ''
            );
            $userInfo   =   $facebook->api($param);
        }
        catch(Exception $o){
            d($o);
        }

        //update user's status using graph api
        if (isset($_POST['tt'])){
            try {
                $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> $_POST['tt'], 'cb' => ''));
            } catch (FacebookApiException $e) {
                d($e);
            }
        }

        //fql query example using legacy method call and passing parameter
        try{
            //get user id
            $uid    = $facebook->getUser();
            //or you can use $uid = $fbme['id'];

            $fql    =   "select name, hometown_location, sex, pic_square from user where uid=" . $uid;
            $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $facebook->api($param);
        }
        catch(Exception $o){
            d($o);
        }
    }
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>PHP SDK & Graph API base FBConnect Tutorial | Thinkdiff.net
    </head>
<body>
    <div id="fb-root">
        <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({appId: '<?=$fbconfig['appid' ]?>', status: true, cookie: true, xfbml: true});

                /* All the events registered */
                FB.Event.subscribe('auth.login', function(response) {
                    // do something with response
                    login();
                });
                FB.Event.subscribe('auth.logout', function(response) {
                    // do something with response
                    logout();
                });
            };
            (function() {
                var e = document.createElement('script');
                e.type = 'text/javascript';
                e.src = document.location.protocol +
                    '//connect.facebook.net/en_US/all.js';
                e.async = true;
                document.getElementById('fb-root').appendChild(e);
            }());

            function login(){
                document.location.href = "";
            }
            function logout(){
                document.location.href = "";
            }
</script>
<style type="text/css">
    .box{
        margin: 5px;
        border: 1px solid #60729b;
        padding: 5px;
        width: 500px;
        height: 200px;
        overflow:auto;
        background-color: #e6ebf8;
    }
</style>

    <h3>PHP SDK & Graph API base FBConnect Tutorial | Thinkdiff.net
    <?php if (!$fbme) { ?>
        You've to login using FB Login Button to see api calling result.
    <?php } ?>
    <p>
        <fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream">
    </p>

    <!-- all time check if user session is valid or not -->
    <?php if ($fbme){ ?>
    <table border="0" cellspacing="3" cellpadding="3">
        <tr>
            <td>
                <!-- Data retrived from user profile are shown here -->
                <div class="box">
                    <b>User Information using Graph API
                    <?php d($fbme); ?>
                </div>
            </td>
            <td>
                <div class="box">
                    <b>User likes these movies | using graph api
                     <?php d($movies); ?>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="box">
                    <b>User Information by Calling Legacy API method "users.getinfo"
                    <?php d($userInfo); ?>
                </div>
            
            <td>
                <div class="box">
                    <b>FQL Query Example by calling Legacy API method "fql.query"
                    <?php d($fqlResult); ?>
                </div>
            </td>
        </tr>
    </table>
    <div class="box">
        <form name="" action="<?=$config['baseurl']?>" method="post">
            <label for="tt">Status update using Graph API
            <br />
            <textarea id="tt" name="tt" cols="50" rows="5">Write your status here and click 'submit'
            <br />
            <input type="submit" value="Update My Status" />
        </form>
        <?php if (isset($statusUpdate)) { ?>
            <br />
            <b style="color: red">Status Updated Successfully! Status id is 
         <?php } ?>
    </div>
    <?php } ?>

    

Upvotes: 2

Views: 1495

Answers (1)

Luke Stevenson
Luke Stevenson

Reputation: 10341

Judging by the fact that your demo page still contains the original content from the ThinkDiff.net website, and in accordance with Ockham's Razor, I will ask - have you updated the contents of the files from the tutorial to reflect the credentials for your own website?

In fbmain.php have you updated

$fbconfig['appid' ]  = "your application id";
$fbconfig['api'   ]  = "your application api key";
$fbconfig['secret']  = "your application secret key";

with your Facebook Application ID, API key and Secret Key?

In index.php have you updated

$config['baseurl']  =   "http://thinkdiff.net/demo/newfbconnect1/php/index.php";

to your own URL for the index.php file's location?

Failure to do these things (specifically the first set) will, of course, cause an authentication error when you try and execute a Facebook Login.


Update - Reviewed the Output of the Demo Page

Firstly, the content of the demo page appears quite different from the demo files associated with the tutorial. Anyway, having looked at the page, I noticed the below line of javascript:

newwindow=window.open('https://www.facebook.com/login.php?api_key=109479982464493&cancel_url=http%3A%2F%2Fthinkdiff.net%2Fdemo%2Fnewfbconnect1%2Fphp%2Findex.php%3Fcancel%3D1&display=popup&fbconnect=1&next=http%3A%2F%2Fthinkdiff.net%2Fdemo%2Fnewfbconnect1%2Fphp%2Findex.php%3Floginsucc%3D1&return_session=1&session_version=3&v=1.0&req_perms=email%2Cuser_birthday','Login_by_facebook',features);

As suspected - the "thinkdiff.net" content is still present in your code somewhere.

Do a search for "thinkdiff" in all of your code, and, as you do, replace it with the correct content.

Upvotes: 2

Related Questions