Python Social Auth多个范围的额外数据

时间:2014-08-28 17:43:14

标签: python django google-oauth

我正在为我们的应用程序使用Python Social Auth,最近我创建了一个双范围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并且它完美地工作,但仍然是自定义后端的nada。非常感谢您提出任何建议。

编辑:尝试了一些不同的方法,但仍然无法设置数据。

1 个答案:

答案 0 :(得分: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