簡體   English   中英

使用OAuth FB登錄后,龍卷風應用程序無法設置cookie

[英]tornado app failing to set cookie after FB login with OAuth

我寫了一個使用FB身份驗證的龍卷風應用程序。 首先,它獲得用戶的許可並獲得訪問令牌。 然后使用訪問令牌再次調用FB,獲取電子郵件ID並將其保存在Cookie中。

以下是處理程序:

class FAuthLoginHandler(tornado.web.RequestHandler, tornado.auth.FacebookGraphMixin):
    @tornado.web.asynchronous
    def get(self):
        user_id = self.get_secure_cookie('trakr')

        if self.get_argument('code', None):
            self.get_authenticated_user(
                redirect_uri=settings.redirect_url,
                client_id=self.settings['facebook_api_key'],
                client_secret=self.settings['facebook_secret'],
                code=self.get_argument('code'),
                callback=self.async_callback(self._on_facebook_login))
            return
        elif self.get_secure_cookie('access_token'):
            self.redirect('/')
            return

        self.authorize_redirect(
            redirect_uri=settings.redirect_url,
            client_id=self.settings['facebook_api_key'],
            extra_params={'scope': 'email'}
        )


    def _on_facebook_login(self, user):                
        if not user:
            self.clear_all_cookies()
            raise tornado.web.HTTPError(500, 'Facebook authentication failed')

        # print "http//graph.facebook.com/%s/picture" % str(user['id'])
        # print "http//graph.facebook.com/%s" % str(user['id'])

        self.set_secure_cookie('fb_id', str(user['id']))
        self.set_secure_cookie('access_token', str(user['access_token']))
        self.facebook_request("/me", access_token=user["access_token"], callback=self.async_callback(self._save_user_profile))
        self.redirect('/')

    def _save_user_profile(self, user):
        if not user:
            raise tornado.web.HTTPError(500, "Facebook authentication failed.")
        # print user
        print user.get('email')
        self.set_secure_cookie('trakr', user.get('email'))
        print self.get_secure_cookie('trakr')

_save_user_profile ,第一個print語句將打印用戶登錄的電子郵件ID。它將成功打印我的電子郵件。 然后我嘗試將其保存在cookie中,但是當我打印時,得到的響應為None

我以為我迷上了使用類,可能是我使用了錯誤的self 我打印idself在幾個地方,似乎是相同的。 而且我還打印了dir(self) ,它確實具有set_secure_cookie函數。 那么為什么無法設置cookie?

您將有機會設置Cookie之前將用戶重定向。 在刪除重定向呼叫_on_facebook_login並將其移動到結束_save_user_profile

另外,設置Cookie后,您將無法立即讀回Cookie-設置Cookie將在下一個請求時生效; get_cookie API返回請求傳入時存在的cookie。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM