2011-10-15 47 views
2

登錄後,我想要顯示文本「Welcome,Niklas」,但登錄後必須重新加載page,我不知道如何使頁面顯示來自服務器變量current_user的文本。如果我登錄並按下重新加載,則會顯示正確的歡迎信息。你能幫我實現我想要的嗎?爲什麼FB python + javascript沒有簡單的工作例子?我可以實現Facebook連接沒有JavaScript?如果是這樣,我必須自己使用並設置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 

回答

2

當使用OAuth 2.0

FB.Init(...oauth=true) 

登錄按鈕設置cookie到您指定的域

fbsr_(appid)=.... 

w其使用應用密鑰進行編碼,並且其不再包含用戶令牌

如果你真的想避免使用java腳本客戶端(這是最簡單的方法),你可以利用這個cookie的存在來知道用戶是否連接到Facebook,然後執行任何授權檢查或動態顯示歡迎消息。

我不使用Python,所以我沒有工作的例子,但是這給你一個搜索的方法。

希望得到這個幫助。