Facebook API网站 - JavaScript SDK - 困惑

时间:2012-12-19 10:26:53

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

我一直在寻找有关使用API​​登录我网站的Facebook指南。现在我已经研究了JS SDK。我是一名PHP程序员,但认为JS SDK会更容易......至少我是这么认为的。我真的很困惑,一切都真的。这是一些简单的问题:

  1. 如果我“只是”不想使用API​​登录我的网站,那么我会不会使用PHP或JS?
  2. 现在 - 如果使用JS - 我如何正确地与Facebook互动?现在它登录到facebook并登录facebook。我不想只是授予我的应用程序访问权限(正常情况下)和注销应用程序。
  3. 如果仍在使用JS - 如果句子确定用户是否已登录,我该怎么做?我的意思是,如果用户登录,我不想显示一些用户信息,并且可以编辑设置。
  4. 总而言之,我想我可能误解了JS SDK的功能还是什么?我真的很困惑,我真的需要一些帮助再次上路。

    *新*

    因此,每次我想向登录用户显示内容时,我都必须使用FB.getLoginStatus();?

2 个答案:

答案 0 :(得分:4)

问:如果我“只是”不想使用API​​登录我的网站,那么我会不会使用PHP或JS?

A-你可以同时使用两者。见 - Login

问:我如何正确地与Facebook互动?

A-用户授权应用程序后,您可以通过API调用使用任何方法(使用您的facebook对象)。仔细阅读 - JS SDK Reference

问:如果句子判断用户是否已登录,我该怎么办?

A-您可以使用FB.getLoginStatus

答案 1 :(得分:1)

我最近制作了这段代码,向朋友介绍Facebook登录功能。每行代码都有详细的注释。我不确定这是否是最好的方法。但这很容易理解。

<?php
//include Facebook library File
include_once "src/facebook.php";
//Application Configurations
//Declare variables app_id,app_secret, we get app_id,app_secret values from app dashboard. 
$app_id     = "xxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Create our application instance
//we create $facebook instance and would use it to access various Facebook library functions
$facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    ));

?>
<html>
<head>
</head>
<body>
<?php
// Get User ID
$user = $facebook->getUser();
//  $facebook->getUser() is a function which would return a user id if someone is
//  logged in  on Facebook currently (In some other window or tab)
// We may or may not have this data based 
// on whether the user is logged in.
// If we have a $user id here, it means we know 
// the user is logged into Facebook,
// but we don’t know if the user has authorized our application.
// i.e whether user has already given our app permissions to access his data or not
if ($user) {
 /*
 * Facebook user retrieved
 * $user : Holds the Facebook Users unique ID
 * */
 try {
        // We try to get users data assuming he has authoized are app if we dont get data we display login button in catch block.
        $userinfo = $facebook->api('/me');
        echo "<br/>User id : ".$userinfo['id'];
        echo "<br/>Name : ".$userinfo['name'];
        echo "<br/>Gender : ".$userinfo['gender'];
        echo "<br/>Email : ".$userinfo['email'];
        echo "<br/>Location : ".$userinfo['location']['name'];
        echo "<br/>Image : <img src='https://graph.facebook.com/".$userinfo['id']."/picture' >";
        echo "<br/><br/><br/>Similarly we can get lot more information about user like work experience, dob etc more details would be shared later /We use this data to prefill registration form for user. ";
        echo "<br/><br/>Below is dump of entire array which shows all available data it carries<br/><br/>";
            echo '<pre>';
            var_dump($userinfo); 
            echo '</pre>';
        //Similarly we can get lot more information about user like work experience, dob etc more details would be shared later
        //We use this data to prefill registration form for user.

    } catch (FacebookApiException $e) {
         // Display Facebook login button - Requires Facebook JavaScript SDK (Below)
         echo '<fb:login-button show-faces="true" width="200" scope="email,user_photos"></fb:login-button>'."\n";
         $user = NULL;
     }
} else {
 // No user logged in currently Display Facebook login button - Requires Facebook JavaScript SDK (Below)
 echo '<fb:login-button show-faces="false" width="200" scope="email,user_photos"></fb:login-button>'."\n";
}
?>
<!--Below code is required to use FAcebook Javascript SDK/Library, Before this we have been using Facbeook PHP-SDK/Library --> 
<div id="fb-root"></div> <!-- A div element with id "fb-root" is required. Facebook JS SDK will auto create this element if it doesn't exist , But we will anyways create it-->
<script>
// window.fbAsyncInit will execute code within it asynchronously i.e without affecting the load time.
window.fbAsyncInit = function () {
//Below code Initializes Javacript SDK, appid is same as one defined in fbaccess.php
 FB.init({
 appId : '<?=$app_id?>',
 cookie : true,
 xfbml : true,
 oauth : true
 });

//Below code tells the script to refresh whenever user logins or logouts. 
 FB.Event.subscribe('auth.login', function (response) { window.location.reload(); });
 FB.Event.subscribe('auth.logout', function (response) { window.location.reload(); });
};

// Copy paste code to load the Facebook JavaScript SDK into the page
(function(){var e=document.createElement('script');e.async=true;e.src=document.location.protocol+'//connect.facebook.net/en_US/all.js';document.getElementById('fb-root').appendChild(e);}());
</script>
</body>
</html>

在Javascript中,您可以调用这样的函数:

function login(){
    FB.getLoginStatus(function(response) {
        if (response.authResponse) {                                    
                alert(response.authResponse.userID);
                //Authenticated user.
        } else {
        // Unauthenticated user display authentication popup
        FB.login(function(response) {
            if (response.authResponse) { 
                    alert(response.authResponse.userID);
                    //User authenticated.
                } 
            else{                                           
                    alert("Please allow authentication to proceed!!");
                }
            }, {scope:'email'});
        }
    });
}