代码之家  ›  专栏  ›  技术社区  ›  Jing

使用python social auth、SqlAlchemy和Flask从Facebook登录重定向时出错

  •  1
  • Jing  · 技术社区  · 8 年前

    我正在尝试使用python社交身份验证在Flask中实现社交登录。它在本地主机上的开发中有效,但在生产中无效。

    从Facebook登录重定向时发生错误。调试之后,它似乎是由sql类型pickle或json中的解码引起的。我挖到了 json 模块,但不知道如何修复它,我认为不应该修复它,因为它是一个内置模块。

    为什么Facebook登录在本地主机上的开发中有效,但在生产中无效?

    Exception on /complete/facebook/ [GET]
    Traceback (most recent call last):
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
        response = self.full_dispatch_request()
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
        rv = self.dispatch_request()
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/apps/flask_app/utils.py", line 46, in wrapper
        return func(backend, *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/apps/flask_app/routes.py", line 23, in complete
        *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/actions.py", line 43, in do_complete
        user = backend.complete(user=user, *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/base.py", line 41, in complete
        return self.auth_complete(*args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/utils.py", line 229, in wrapper
        return func(*args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/facebook.py", line 87, in auth_complete
        return self.do_auth(access_token, response, *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/facebook.py", line 119, in do_auth
        return self.strategy.authenticate(*args, **kwargs)
    File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/base.py", line 82, in authenticate
        return self.pipeline(pipeline, *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/base.py", line 85, in pipeline
        out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/backends/base.py", line 112, in run_pipeline
        result = func(*args, **out) or {}
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/pipeline/social_auth.py", line 20, in social_user
        social = backend.strategy.storage.user.get_social_auth(provider, uid)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/social/storage/sqlalchemy_orm.py", line 136, in get_social_auth
        uid=uid)[0]
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2462, in __getitem__
        return list(self[item:item + 1])[0]
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2457, in __getitem__
        return list(res)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 86, in instances
        util.raise_from_cause(err)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 200, in raise_from_cause
        reraise(type(exception), exception, tb=exc_tb, cause=cause)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 71, in instances
        rows = [proc(row) for row in fetch]
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 428, in _instance
        loaded_instance, populate_existing, populators)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 486, in _populate_full
        dict_[key] = getter(row)
      File "/home/dev/huugbaan/xxvenv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py", line 1258, in process
        return loads(value)
      File "/usr/local/lib/python2.7.12/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/usr/local/lib/python2.7.12/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    TypeError: expected string or buffer
    [pid: 5053|app: 0|req: 6/26] 171.99.3.44 () {42 vars in 1892 bytes} [Sun Jul 17 02:09:00 2016] GET /complete/facebook/?redirect_state=i2C8teuxp7n3o39bhzK8Ypwa8S2O1EzC&code=AQBim0qoaEqukud-ysEQpuhyhy63v729wxqQ5qo3HTYRFSerzQrCgjXVKUu-EA1A1A5CXbAfzvZHb87K4moaxt0Wk4g5mhQQQ--ovb9NygFUybyznSk-09O8x-zB5CXSyFVR-fDV54CXSNbhBOJJvPWQu9rJ0QlGj53kEDFyKCgXA0-gHyLIXfDveY-3Z_nouWv0mosSogs9JSE06EQ9ZfZh87HKJkihEcnAs_QhO3eHhkZ5uqf7ZSWc-Soj2c4XXVEG38aR5Ltl-cv7tWkwAsKFtW36nBcF0NzEhDiuSYaiTIn-nZBnRBveO7PFrDJRj6GEts1tc9pY29KnZuA72YwN&state=i2C8teuxp7n3o39bhzK8Ypwa8S2O1EzC => generated 4571 bytes in 587 msecs (HTTP/1.1 500) 2 headers in 100 bytes (1 switches on core 0)
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Jing    8 年前

    只需在sqltype上执行脏修复。sqlalchemy包的py 调试后。我在下面的方法中找到了不同类型的值。所以我决定跳过并返回已经是dict的值。

    1245     def result_processor(self, dialect, coltype):
    1246         impl_processor = self.impl.result_processor(dialect, coltype)
    1247         loads = self.pickler.loads
    1248         if impl_processor:
    1249             def process(value):
    1250                 value = impl_processor(value)
    1251                 if value is None:
    1252                     return None
    1253                 return loads(value)
    1254         else:
    1255             def process(value):
    1256                 if value is None:
    1257                     return None
    #### ->>>            if type(value) is dict: return value
    1258                 return loads(value) 
    1259         return process
    

    if type(value) is dict: return value