Python 社交认证多个范围额外数据
我正在为我们的应用使用Python Social Auth,最近创建了一个支持两个权限的Google登录。我的想法是让用户可以用Google账号登录,然后再提升权限,以便我们可以读取他们的邮件并从中提取相关内容。我按照文档中的建议操作:http://psa.matiasaguirre.net/docs/use_cases.html#multiple-scopes-per-provider,创建了一个新的后端:
from social.backends.google import GoogleOAuth2
class GoogleEmailOAuth2(GoogleOAuth2):
name = 'google-email'
然后把必要的部分添加到我们的settings.py文件中(还有所有需要的URL):
SOCIAL_AUTH_GOOGLE_EMAIL_SCOPE = ['email', 'profile', 'https://www.googleapis.com/auth/plus.profile.emails.read']
SOCIAL_AUTH_GOOGLE_EMAIL_AUTH_EXTRA_ARGUMENTS = {'access_type': 'offline'}
SOCIAL_AUTH_GOOGLE_EMAIL_EXTRA_DATA = [
('id', 'user_id'),
('email', 'user_email'),
('refresh_token', 'refresh_token', True),
('expires_in', 'expires'),
('token_type', 'token_type', True)
]
一切看起来都正常,除了创建的UserSocialAuth对象在extra_data里什么都没有……我用正常的SOCIAL_AUTH_GOOGLE_OAUTH2_EXTRA_DATA设置了相同的权限,它工作得很好,但自定义后端却还是没有数据。非常感谢任何建议。
编辑:尝试了几种不同的方法,但还是无法设置数据。
1 个回答
0
我没有遇到同样的问题,所以不能直接回答这个问题,但我有一个建议:可以在load_extra_data
这个管道函数里设置一个调试断点,然后从那里开始跟踪。
def load_extra_data(backend, details, response, uid, user, *args, **kwargs):
#
# <-- Add your debugger of choice, e.g.
# import pdb; pdb.set_trace()
#
social = kwargs.get('social') or \
backend.strategy.storage.user.get_social_auth(backend.name, uid)
if social:
extra_data = backend.extra_data(user, uid, response, details,
*args, **kwargs)
social.set_extra_data(extra_data)
https://github.com/omab/python-social-auth/blob/v0.2.13/social/pipeline/social_auth.py#L82-L88