如何在登录/注销时重新加载此页面?

时间:2022-06-28 01:23:22

I tried to make the user variable available serveside and now I've noticed that my page behaves the way I want it if I reload:

我试着让用户变量可用,现在我注意到如果我重新加载,我的页面就像我想要的那样:

如何在登录/注销时重新加载此页面?

So how can I force the page to reload at facebook connect / disconnect? Any other idea about this code? I tried the obvious javascript but that created a loop. Can you help me? Thanks

那么如何强制页面在facebook连接/断开连接时重新加载?有关此代码的任何其他想法?我尝试了明显的javascript但是创建了一个循环。你可以帮我吗?谢谢

{% 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>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=164355773607006";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script><div class="fb-login-button" autologoutlink="true" data-show-faces="true" data-width="200" data-max-rows="1"></div>
{% if greeting or user or current_user or twittername %}
<div id="user-ident">

<span>{% trans "Welcome," %} <b>{{user.nickname}}{% if not user %}{{ current_user.name|escape }}{% endif %}{% if not user and not current_user %}{{ twittername }}{% endif %}</b></span>

</div>
 <fb:login-button autologoutlink="true">Logout</fb:login-button>        
 {% else %}
 <fb:login-button autologoutlink="true">Login with Facebook</fb:login-button>{% endif %}
</body> 
</html>

Update

I removed all Javascript and cookies since they are not needed and did a pure python solution with OAuth 2.0 to enable my scenario "login with facebook":

我删除了所有的Javascript和cookie,因为它们不需要,并使用OAuth 2.0做了一个纯python解决方案,以使我的场景“登录facebook”:

How to add Facebook as OAuth 2.0 provider: Here's how I make "Login with facebook" for my website with OAuth instead of javascript / cookie this is python only for OAuth 2.0 with Facebook and as far as I can tell it's working:

如何将Facebook添加为OAuth 2.0提供者:以下是我使用OAuth而不是javascript / cookie为我的网站制作“使用facebook登录”的方法这只是针对OAuth 2.0和Facebook的python,据我所知,它的工作原理如下:

class FBUser(db.Model):
    id = db.StringProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True)
    name = db.StringProperty(required=True)
    profile_url = db.StringProperty()
    access_token = db.StringProperty(required=True)
    name = db.StringProperty(required=True)
    picture = db.StringProperty()
    email = db.StringProperty()
    friends = db.StringListProperty()

class I18NPage(I18NHandler):
    def get(self):
    if self.request.get('code'):
          args = dict(
            code = self.request.get('code'),
            client_id = facebookconf.FACEBOOK_APP_ID,
            client_secret = facebookconf.FACEBOOK_APP_SECRET,
            redirect_uri = 'http://www.koolbusiness.com/',
          )
      logging.debug("client_id"+str(args))
          file = urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args))
          try:
        logging.debug("reading file")
            token_response = file.read()
        logging.debug("read file"+str(token_response))
          finally:
            file.close()
          access_token = cgi.parse_qs(token_response)["access_token"][-1]
          graph = main.GraphAPI(access_token)
          user = graph.get_object("me")   #write the access_token to the datastore
      fbuser = main.FBUser.get_by_key_name(user["id"])
          logging.debug("fbuser "+str(fbuser))

          if not fbuser:
            fbuser = main.FBUser(key_name=str(user["id"]),
                                id=str(user["id"]),
                                name=user["name"],
                                profile_url=user["link"],
                                access_token=access_token)
            fbuser.put()
          elif fbuser.access_token != access_token:
            fbuser.access_token = access_token
            fbuser.put()

The login link is

登录链接是

<a href="https://www.facebook.com/dialog/oauth?client_id=164355773607006&redirect_uri=http://{{host}}/"><img src="/_/img/loginwithfacebook.png"></a> that redirects and allows me to pick up the access_token in the method above and logout is straightforward:

如何在登录/注销时重新加载此页面?重定向并允许我在上面的方法中获取access_token并且注销很简单:

<a href="https://www.facebook.com/logout.php?next=http://www.koolbusiness.com&access_token={{access_token}}">{% trans "Log out" %}</a>

{%trans“退出”%}

1 个解决方案

#1


1  

If your page content has dependencies on events that occur after the pageload (ie Facebook login/logout) your best bet may be to use jQuery + ajax to load and/or reload the dependent elements separately.

如果您的页面内容依赖于页面加载后发生的事件(即Facebook登录/注销),您最好的选择可能是使用jQuery + ajax分别加载和/或重新加载依赖元素。

#1


1  

If your page content has dependencies on events that occur after the pageload (ie Facebook login/logout) your best bet may be to use jQuery + ajax to load and/or reload the dependent elements separately.

如果您的页面内容依赖于页面加载后发生的事件(即Facebook登录/注销),您最好的选择可能是使用jQuery + ajax分别加载和/或重新加载依赖元素。