如何将我的网站整合到Facebook

时间:2011-03-11 12:34:39

标签: php facebook authentication

我想将我的网站集成到Facebook登录系统。我用谷歌搜索了它,最后我找到了great tutorial

但我的身份验证存在问题。 Here您可以找到我的测试网站。我用过php-sdk;请在我犯错的地方帮忙。

<?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 } ?>

    

1 个答案:

答案 0 :(得分:2)

根据您的演示页面仍包含来自ThinkDiff.net网站的原始内容,并根据Ockham's Razor,我会问 - 您是否更新了教程中文件的内容以反映您自己网站的凭据?

fbmain.php 中您更新了

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

使用您的Facebook应用程序ID,API密钥和密钥?

index.php 中您更新了

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

到index.php文件位置的自己的URL?

当您尝试执行Facebook登录时,未能执行这些操作(特别是第一组)将导致身份验证错误。


更新 - 检查演示页面的输出

首先,演示页面的内容与本教程相关的演示文件有很大不同。无论如何,看了一下页面后,我注意到以下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);

如所怀疑的那样,“thinkdiff.net”内容仍然存在于您的代码中。

在您的所有代码中搜索“thinkdiff”,并且正如您所做的那样,将其替换为正确的内容。