【问题标题】:Facebook API Website - JavaScript SDK - ConfusedFacebook API 网站 - JavaScript SDK - 困惑
【发布时间】:2012-12-19 10:26:52
【问题描述】:

我一直在浏览 Facebook 的关于使用他们的 API 登录我的网站的指南。现在我已经研究了 JS SDK。我是一名 PHP 程序员,但认为 JS SDK 会更容易……至少我是这么认为的。我真的很困惑,一切真的。以下是一些简单的问题:

  1. 如果我“只是”不想使用 API 来登录我的网站,而不是其他,那么我会使用 PHP 还是 JS?
  2. 现在 - 如果使用 JS - 如何正确与 Facebook 交互?现在它正在登录facebook并退出facebook。我不想只是授予对我的应用程序的访问权限(像正常一样)并注销应用程序。
  3. 如果仍在使用 JS - 如何判断用户是否登录?我的意思是,如果用户登录,我不想显示一些用户信息,以及编辑设置的可能性。

总而言之,我想我可能误解了 JS SDK 的功能还是什么?我真的很困惑,我真的需要一些帮助才能重新上路。

* 新 *

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

【问题讨论】:

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


    【解决方案1】:

    Q- 如果我“只是”不想使用 API 登录我的网站,而没有别的,那么我会使用 PHP 还是 JS?

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

    Q- 如何正确与 Facebook 互动?

    A- 一旦用户授权应用程序,您可以通过 API 调用使用任何方法(使用您的 facebook 对象)。通过这个-JS SDK Reference

    Q- if 语句如何判断用户是否登录?

    A- 你可以使用FB.getLoginStatus

    【讨论】:

    • 请看我的问题,我会补充一些。
    • 不完全是。如果你从 FB.login 中得到 response.authResponse,这意味着用户已经成功登录,那么你可以使用任何你想要的功能。您甚至可能不需要调用 FB.getLoginStatus。一切由您决定如何进行。
    • 我的意思是:如果我不想显示登录用户上传到我的页面的图像(不是来自 fb 的图像),我该怎么做?我很迷茫。我不需要像 php 中这样的 if 语句吗: if($_SESSION['loggedIn']){ //show images }
    • 你看,我还是说用户登录成功后,调用任意方法 eg:FB.api("/me/accounts");等我不确定您是否可以获取用户上传的图片。您可以在这里进行测试:developers.facebook.com/tools/explorer 在框中写我/帐户,然后从结果中选择页面。
    • 嗯,我在说,当用户通过facebook登录时,他必须在我的网站上有一个帐户,否则我无法为他设置,对吗?他需要某种 id 来获取联系信息等。我该怎么做?
    【解决方案2】:

    我最近编写了这段代码来向一位朋友解释 Facebook 登录功能。每行代码都有详细的 cmets。我不确定这是否是最好的方法。但这很容易理解。

    <?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'});
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多