繁体   English   中英

Facebook API网站-JavaScript SDK-困惑

[英]Facebook API Website - JavaScript SDK - Confused

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

  1. 如果我“只是”不想使用API​​登录我的网站,而没有其他选择,那我会使用PHP还是JS?
  2. 现在-如果使用JS-如何正确与Facebook互动? 现在,它正在登录Facebook,然后从Facebook注销。 我所要做的就是只是授予对我的应用程序的访问权限(如正常)并注销该应用程序。
  3. 如果仍在使用JS-如何使句子确定用户是否已登录? 我的意思是,如果用户登录,则不会显示某些用户信息,也无法显示设置。

总而言之,我认为我可能误解了JS SDK的功能或什么? 我真的很困惑,我真的需要一些帮助才能再次上路。

*新*

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

问:如果我“只是”不想使用API​​登录我的网站,而没有别的选择,那我会使用PHP还是JS?

答:您可以同时使用。 查看登录

问:如何与Facebook正确互动?

答:一旦用户授权了该应用程序,您就可以通过API调用(使用您的facebook对象)使用任何方法。 仔细阅读-JS SDK参考

问:如果句子确定用户是否登录,我该怎么做?

答:您可以使用FB.getLoginStatus

我最近编写了这段代码,向朋友介绍了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'});
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM