python-social-auth: AttributeError: 'SQLAlchemy' 对象在/complete/vk-oauth2 url 上没有属性 'query'

标签 python flask sqlalchemy python-social-auth

我有一个 Flask Web 应用程序。我尝试集成 python-social-auth。

我设置:

SOCIAL_AUTH_USER_MODEL = 'app.models.User'
SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
    'social.backends.vk.VKOAuth2',
)
SOCIAL_AUTH_VK_OAUTH2_KEY = '***'
SOCIAL_AUTH_VK_OAUTH2_SECRET = '***'
SOCIAL_AUTH_VK_OAUTH2_SCOPE = []

当我访问 URL /login/vk-oauth2 并在 VK (vk.com) 中成功授权后,我会重定向到 /complete/vk-oauth2/?redirect_state =123ewq&code=123&state=123qwe 出现错误:AttributeError: 'SQLAlchemy' 对象没有属性 'query'

回溯:

Traceback (most recent call last):
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "my_app/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "my_app/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "my_app/env/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "my_app/env/lib/python3.4/site-packages/social/apps/flask_app/utils.py", line 46, in wrapper
    return func(backend, *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/apps/flask_app/routes.py", line 23, in complete
    *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/actions.py", line 44, in do_complete
    user = backend.complete(user=user, *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/base.py", line 41, in complete
    return self.auth_complete(*args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/utils.py", line 246, in wrapper
    return func(*args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/oauth.py", line 397, in auth_complete
    *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/utils.py", line 246, in wrapper
    return func(*args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/oauth.py", line 408, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/strategies/base.py", line 150, in authenticate
    return backend.authenticate(*args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/base.py", line 82, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/base.py", line 85, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "my_app/env/lib/python3.4/site-packages/social/backends/base.py", line 112, in run_pipeline
    result = func(*args, **out) or {}
  File "my_app/env/lib/python3.4/site-packages/social/pipeline/social_auth.py", line 20, in social_user
    social = backend.strategy.storage.user.get_social_auth(provider, uid)
  File "my_app/env/lib/python3.4/site-packages/social/storage/sqlalchemy_orm.py", line 141, in get_social_auth
    return cls._query().filter_by(provider=provider,
  File "my_app/env/lib/python3.4/site-packages/social/storage/sqlalchemy_orm.py", line 39, in _query
    return cls._session().query(cls)
AttributeError: 'SQLAlchemy' object has no attribute 'query'

我的用户模型:

from app import app
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), nullable=True)
    provider = db.Column(db.String(50))
    social_id = db.Column(db.Integer)
    created_at = db.Column(db.DateTime, server_default=func.now(), nullable=False)

    @property
    def is_active(self):
        return True

    @property
    def is_authenticated(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def get_id(self):
        return str(self.id)

使用过的包:

$ pip freeze
alembic==0.8.6
defusedxml==0.4.1
Flask==0.10.1
Flask-Cors==2.1.2
Flask-Fixtures==0.3.3
Flask-Login==0.3.2
Flask-Migrate==1.8.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.1
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.4
MarkupSafe==0.23
oauthlib==2.0.0
psycopg2==2.6.1
PyJWT==1.4.2
python-editor==1.0
python-social-auth==0.2.21
python3-openid==3.0.10
requests==2.10.0
requests-oauthlib==0.7.0
six==1.10.0
SQLAlchemy==1.0.13
Werkzeug==0.11.9

我做错了什么?或者我没有做什么?

最佳答案

您是否按照文档中所述使用 init_app 函数? http://psa.matiasaguirre.net/docs/configuration/flask.html#models-setup

该部分不正确,应该说您需要将 SQLAlchemy session 传递给 init_app 函数,而不是对数据库的引用。

应该是这样的:

init_app(app, db.session)

我已提交 PR 来解决此问题。 https://github.com/omab/python-social-auth/pull/1050

关于python-social-auth: AttributeError: 'SQLAlchemy' 对象在/complete/vk-oauth2 url 上没有属性 'query',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933816/

相关文章:

python - 多个函数调用如何在 python 2.7 中工作

python - 如何加快Flask响应下载速度

javascript - 在 ReactJS 中显示来自 flask send_file 函数的图像

python - 为什么在微型 df 上使用 fast_executemany 会出现内存错误?

python - 类型错误 : Object of type 'datetime' is not JSON serializable (with serialize function)

python - 如何在 Pygame 中克隆 Sprite ?

python - 使用 pandas 和 matplotlib 绘制分类数据

python - 在前进到下一个单元之前,强制 matplotlib 在 IPython/Jupyter 笔记本单元中完全渲染绘图

python - 将 Flask env vars 添加到 virtualenv 的激活脚本可以吗?

python - 使用 SqlAlchemy 在 Python 中检索外键映射对象