2014-09-05 98 views

回答

2

有Facebook的非官方SDK(https://github.com/pythonforfacebook/facebook-sdk)。它對於AppEngine來說非常有用,其中用戶通過html/js按鈕登錄,並從FB cookie獲取有關服務器端用戶的數據。 在GitHub上的示例代碼中,您可以很容易地實現代碼。

通過使用得到FB的cookie的用戶:

fb_user = facebook.get_user_from_cookie(request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET) 

其中Request.Cookies時是含有請求中的Cookie字典(如在GAE的webapp2的)

當你有fb_user就可以開始檢索有關數據他/她:

graph = facebook.GraphAPI(fb_user["access_token"]) 
profile = graph.get_object("me") 
user_email = profile["email"] 

facebook.GraphAPI使用用戶的訪問令牌來授權您的來自Facebook的請求。然後你選擇對象「我」,這是用戶使用你的應用程序。稍後,您可以根據用戶登錄的登錄按鈕範圍使用您有權使用的任何數據。

如果您正在使用webapp2 additionaly,則可以創建自定義Handler類以保存用戶數據要在當前會話中使用

from webapp2_extras import sessions # import session 
config = {} 
config['webapp2_extras.sessions'] = dict(secret_key='') 

# remember to pass config to webapp2.WSGIApplication: 
app = webapp2.WSGIApplication(
    [('/', HomeHandler), ('/logout', LogoutHandler)], #handlers 
    debug=True, # debug state 
    config=config # CONFIG! 
) 

class MyHandler(webapp2.RequestHandler): 
    # ... 
    @property 
    def current_user(self): 
     # check if user logged in during this session 
     if self.session.get("user"): 
      # user is logged in 
      return self.session.get("user") 
     else: 
      # either user just logged in or just saw the first page 
      # we'll see here 
      fb_user = facebook.get_user_from_cookie(self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET) 
      if fb_user: 
       # okay so user logged in. 
       # now, check to see if existing user 

       graph = facebook.GraphAPI(fb_user["access_token"]) 
       profile = graph.get_object("me") 

       # DB # user = User.get_by_key_name(PREFIX+fb_user["uid"]) 
       # NDB # user = ndb.Key("User",PREFIX+str(profile["id"])).get() 
       # you can add prefix if you're using multiple social networks (g+, twitter) 
       if not user: 
        # not an existing user so get user info 
        # DB # user = User(key_name = PREFIX+str(profile["id"]), OTHER DATA) 
        # NDB # user = User(id = PREFIX+str(profile["id"]), OTHER DATA) 
        user.put() 
       elif user.SOMETHING_THAT_MIGHT_CHANGE != fb_user["SOMETHING_THAT_MIGHT_CHANGE"]: 
        # update values that changed since las check to database 
        user.SOMETHING_THAT_MIGHT_CHANGE = fb_user["SOMETHING_THAT_MIGHT_CHANGE"] 
        user.put() 
       # User is now logged in 
       # Save often used values to session so you don't have to query database all the time 
       self.session["user"] = dict(
        id = "f"+str(profile["id"]), 
        email = profile["email"], 
        access_token = fb_user["access_token"] 
       ) 
      return self.session.get("user") 
    return None 

    def dispatch(self): 
     self.session_store = sessions.get_store(request=self.request) 
     try: 
      webapp2.RequestHandler.dispatch(self) 
     finally: 
      self.session_store.save_sessions(self.response) 

    @webapp2.cached_property 
    def session(self): 
     return self.session_store.get_session() 

現在你可以使用你的處理程序和訪問當前用戶:

class MainPageHandler(Handler): 
    def get(self): 
     current_user = self.current_user 
     graph = facebook.GraphAPI(self.current_user['access_token']) 

請記住,這僅僅是基礎知識,你可以學到更多的https://github.com/pythonforfacebook/facebook-sdk

+0

謝謝,我會嘗試使用這個。 – 2014-09-06 03:50:44

+0

我不能得到這個工作,因爲它需要請求庫,並且全新的請求代碼下載失敗,導致錯誤'ImportError:No module named'requests.packages.urllib3'(截至2015年1月29日)。 – Chris 2015-01-29 19:20:40

+0

@Chris是的,所以也安裝urllib3。這不是Python內置的urllib2,它是第三方庫:https://urllib3.readthedocs.org/en/latest/ – Hejazzman 2015-01-30 05:44:49

相關問題