如何让欢迎文本显示?

2 投票
1 回答
1492 浏览
提问于 2025-04-17 04:21

在登录之后,我想显示“欢迎,Niklas”这段文字,但每次登录后我都得刷新一下页面,我不太明白怎么才能让页面直接显示服务器变量current_user里的内容。如果我登录后按刷新键,那么正确的欢迎信息就会出现。你能帮我实现我想要的效果吗?为什么没有简单的示例可以展示FB的Python和JavaScript结合使用?我能在不使用JavaScript的情况下实现Facebook连接吗?如果可以的话,我是不是需要自己使用和设置cookie?谢谢你

{% load i18n %}
<!DOCTYPE html> 
<html xmlns:fb="https://www.facebook.com/2008/fbml">
  <head> 
    <title> 
      Test Facebook SDK
    </title> 
  </head> 
<body> 
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '164355773607006', // App ID
      channelURL : '//WWW.KOOLBUSINESS.COM/static/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });
  };
  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>
<fb:login-button autologoutlink="true"></fb:login-button>
{% if current_user %}
<div id="user-ident">
<span>{% trans "Welcome," %} <b>{{ current_user.name|escape }}</b></span>
</div>
{% endif %}
</body> 
</html>

这是我获取变量current_user的方法

@property
def current_user(self):
    if not hasattr(self, "_current_user"):
        self._current_user = None
        cookie = facebook.get_user_from_cookie(
            self.request.cookies, facebookconf.FACEBOOK_APP_ID, facebookconf.FACEBOOK_APP_SECRET)
        logging.debug("logging cookie"+str(cookie))
        if cookie:
            # Store a local instance of the user data so we don't need
            # a round-trip to Facebook on every request
            user = FBUser.get_by_key_name(cookie["uid"])
            logging.debug("user "+str(user))
            logging.debug("username "+str(user.name))

            if not user:
                graph = facebook.GraphAPI(cookie["access_token"])
                profile = graph.get_object("me")
                user = FBUser(key_name=str(profile["id"]),
                            id=str(profile["id"]),
                            name=profile["name"],
                            profile_url=profile["link"],
                            access_token=cookie["access_token"])
                user.put()
            elif user.access_token != cookie["access_token"]:
                user.access_token = cookie["access_token"]
                user.put()
            self._current_user = user
    return self._current_user

1 个回答

2

在使用 Oauth 2.0 的时候,

FB.Init(...oauth=true) 

登录按钮会在你的域名下设置一个名为

fbsr_(appid)=....

的 cookie,这个 cookie 是用 应用的秘密密钥 编码的,并且里面不再包含 用户令牌

如果你真的想避免使用 JavaScript 在客户端(这是最简单的方法),你可以利用这个 cookie 的存在来判断用户是否已经登录 Facebook,然后进行任何授权检查,或者动态显示欢迎信息。

我不使用 Python,所以没有现成的例子,但这给你提供了一个搜索的方向。

希望这对你有帮助。

撰写回答