1

我是使用 Facebook 应用程序环境的新手。我正在开发一个小型应用程序。我正在尝试在我的项目中使用一些社交数据来检查一些条件。我不知道我是否以正确的方式访问 FB 返回的 JSON 对象,但这是我的问题。

我想从当前文件将名字发送到另一个 jsp 或 servlet。因此,我创建了 jsp 变量并尝试在从 Facebook 获得的响应对象中分配“名称”的值。

尝试在 jsp 文件末尾打印名字时,名字输出为空。如何在对象中使用名称和性别值并将其发送到另一个 jsp 文件或 servlet 文件?

敲我的头寻找解决方案。有人请帮忙!

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                       "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Facebook authentication usage</title>
  </head>
  <body>

  <%! String firstname="";
        String lastname="";
        String middlename="lynne";

    %>



  <div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
                                        // init the FB JS SDK
                                        FB.init({
                                        appId      : '532238706811106', // App ID from the App Dashboard
                                        //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
                                        status     : true, // check the login status upon init?
                                        cookie     : true, // set sessions cookies to allow your server to access the session?
                                        frictionlessRequests : true,
                                        xfbml      : true  // parse XFBML tags on this page?

                                            });

    // Additional initialization code such as adding Event Listeners goes here


    FB.getLoginStatus(function(response) {
                                            if (response.status === 'connected') 
                                            {

                                                // the user is logged in and has authenticated your
                                                // app, and response.authResponse supplies
                                                // the user's ID, a valid access token, a signed        
                                                // request, and the time the access token 
                                                // and signed request each expire   
                                                var uid = response.authResponse.userID;
                                                var accessToken = response.authResponse.accessToken;
                                                 FB.api('/me', function(response) 
                                                            {
                                                                console.log(response); 
                                                                console.log('Good to see you, ' + response.name + '.');
                                                                 firstname=firstname+response.name;
                                                                 lastname=lastname+response.last_name;
                                                                 document.write(response.name+middlename);
                                                            });
                                            } 
                                            else if (response.status === 'not_authorized') 
                                            {
                                                // the user is logged in to Facebook, 
                                                // but has not authenticated your app
                                                var oauth_url = 'https://www.facebook.com/dialog/oauth/';
                                              oauth_url += '?client_id=532238706811106'; //Your Client ID
                                              oauth_url += '&redirect_uri=' + 'https://apps.facebook.com/newjsp'; //Send them here if they're not logged in
                                              //oauth_url += '&scope=user_about_me,email,user_location,user_photos,publish_actions,user_birthday,user_likes';

                                              window.top.location = oauth_url;

                                            } 
                                            else 
                                            {
                                                // the user isn't logged in to Facebook.

                                                window.top.location ='https://www.facebook.com/index.php';
                                            }
                        });

  };
  // Load the SDK's source Asynchronously
  // Note that the debug version is being actively developed and might 
  // contain some type checks that are overly strict. 
  // Please report such bugs using the bugs tool.
  (function(d, debug)
          {
                var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
                ref.parentNode.insertBefore(js, ref);
            }(document, /*debug*/ false));


</script>
  <h1>this is a sample to test facebook authentication
        Welcome <%=firstname+""+lastname+middlename%></h1>

        <%out.print(firstname+""+lastname+""+middlename); %>

   </body>
</html> 
4

1 回答 1

0

这是因为您设置firstname""但从不更改该值。

在您的 JavaScript 代码中,firstname=firstname+response.name;不设置先前在您的 Java 代码中定义的 String 变量。相反,它设置了一个只能由您的 JavaScript 代码看到的新变量。

您的 Java 代码在服务器端运行,而 JavaScript 在客户端运行。它们是两个完全不同的程序,在完全不同的位置在不同的时间执行。出于诸如此类的原因,不建议在同一个文件中同时编写客户端和服务器端代码。

至于您的问题的解决方案,我建议您为 Facebook 寻找一个 Java API,以便您可以在呈现 JSP 之前使用它,或者您可以仅在 JavaScript 代码中访问从 FB 返回的数据。阅读 Java、JavaScript、服务器端和客户端代码之间的差异可能也是一个好主意。

于 2013-03-08T11:15:49.450 回答