如何在登录/注销时重新加载此页面? [英] How to make this page reload on login / logout?
问题描述
我试图使用户变量可用的服务,现在我注意到我的页面行为如果我重新加载,我想要的方式:
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:
那么如何强制该页面重新加载在Facebook连接/断开连接?关于这段代码的任何其他想法?我尝试了明显的JavaScript,但是创建了一个循环。你可以帮我吗?感谢
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
{% 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>
更新
我删除了所有的Javascript和cookie,因为它们不需要,并使用了一个纯粹的Python解决方案与OAuth 2.0,以启用我的方案用Facebook登录:
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":
如何添加Facebook作为OAuth 2.0提供商:这是我如何使用OAuth而不是javascript / cookie做为我的网站登录,这只是为了OAuth 2.0与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:
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()
登录链接是
< a href =https://www.facebook.com/dialog/oauth?client_id=164355773607006&redirect_uri=http://{{host}}/ >< img src =/ _ / img / loginwithfacebook.png>< / a>
重定向并允许我在上面的方法中获取access_token,注销是直接的:
<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:
< a href =https://www.facebook.com/logout.php?next=http://www.koolbusiness.com&access_token={{access_token }}> {%trans注销%}< / a>
推荐答案
>如果您的页面内容依赖于在页面加载之后发生的事件(即Facebook登录/注销),您最好的选择是使用jQuery + ajax分别加载和/或重新加载依赖元素。
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.
这篇关于如何在登录/注销时重新加载此页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!