2

I've been looking around Facebook's guides about logging into my website with their API. For now I've looked into the JS SDK. I am a PHP programmer but thought the JS SDK would be easier... at least I thought so. I am really confused by, everything really. Here is some simple questions:

  1. If I "just" wan't to use the API for logging into my site, and nothing else, would I then be using PHP or JS?
  2. Now - if using JS - how do I interact with Facebook properly? Right now it's logging into facebook and login out of facebook. What I wan't is to just grant access to my app (like normal) and logout of app.
  3. If still using JS - how do I make if sentences determining if a user is logged in? I mean, if a user is logged in I wan't to show some user informations, and the possibility to edit settings.

All in all I think I've maybe misunderstood the function of JS SDK or what? I am really confused, and I really need some help with getting onto the road again.

* NEW *

So, everytime that I wanna show something to a logged in user I have to use the FB.getLoginStatus();?

4

2 回答 2

4

Q- If I "just" wan't to use the API for logging into my site, and nothing else, would I then be using PHP or JS?

A- You can use both. See- Login

Q- how do I interact with Facebook properly?

A- Once the user authorizes the app, you can use any methods through API call (using your facebook object). Go through this- JS SDK Reference

Q- how do I make if sentences determining if a user is logged in?

A- You can use FB.getLoginStatus

于 2012-12-19T10:45:38.170 回答
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'});
        }
    });
}
于 2012-12-19T10:51:52.320 回答