python-social-auth: AttributeError: 'SQLAlchemy' object has no attribute 'query' on /complete/vk-oauth2 url
python-social-auth: AttributeError: 'SQLAlchemy' object has no attribute 'query' on /complete/vk-oauth2 url
我有一个 Flask 网络应用程序。我尝试整合 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' object has no attribute '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 会话传递给 init_app
函数而不是对数据库的引用。
应该是这样的:
init_app(app, db.session)
我已经提交了 PR 来解决这个问题。
https://github.com/omab/python-social-auth/pull/1050
我有一个 Flask 网络应用程序。我尝试整合 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' object has no attribute '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 会话传递给 init_app
函数而不是对数据库的引用。
应该是这样的:
init_app(app, db.session)
我已经提交了 PR 来解决这个问题。 https://github.com/omab/python-social-auth/pull/1050