Python Social Auth,扩展断开管道

时间:2015-06-25 00:47:45

标签: python django python-social-auth

我有一个用户个人资料表,它有fb_id,tw_id。所以我正在检查用户是否连接了他们的社交帐户。当用户断开其中一个帐户时,我需要更新这些列。

SOCIAL_AUTH_DISCONNECT_PIPELINE = (
    'social.pipeline.disconnect.allowed_to_disconnect',
    'social.pipeline.disconnect.get_entries',
    'social.pipeline.disconnect.revoke_tokens',
    'social.pipeline.disconnect.disconnect',
    'profiles.utils.disconnect',
)

所以我在我的个人资料应用中添加了一个名为disconnect的函数,因为它引用了here

def disconnect(backend, user, response, *args, **kwargs):
    if Profiles.objects.filter(user=user).exists():
        if backend.name == 'facebook':
            profile=Profiles.objects.get(user=user)
            profile.fb_id = ''
            profile.save()

        if backend.name == 'twitter':
            profile=Profiles.objects.get(user=user)
            profile.tw_id = ''
            profile.save()

        if backend.name == 'instagram':
            profile=Profiles.objects.get(user=user)
            new_profile.in_id = ''
            profile.save()

当我尝试断开帐户时,我收到此错误:

TypeError at /disconnect/facebook/
disconnect() takes at least 3 arguments (2 given)
Request Method: POST
Request URL:    /disconnect/facebook/
Django Version: 1.8.2
Exception Type: TypeError
Exception Value:    
disconnect() takes at least 3 arguments (2 given)

这是我第一次使用python_social_auth,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

试试这个

SOCIAL_AUTH_DISCONNECT_PIPELINE = (
    'social.pipeline.disconnect.allowed_to_disconnect',
    'social.pipeline.disconnect.get_entries',
    'social.pipeline.disconnect.revoke_tokens',
    # 'social.pipeline.disconnect.disconnect',  # comment
    'profiles.utils.custom_pipeline',
)

<强> custom_pipeline.py

def disconnect(strategy, entries, user_storage, *args, **kwargs):        
    for entry in entries:
        user_storage.disconnect(entry)
    backend = kwargs.get('name')

    profile=Profiles.objects.get(user=entries[0].user)
    if backend == 'facebook':
        profile.fb_id = ''
    elif backend == 'twitter':
        profile.tw_id = ''
    elif backend == 'instagram':
        new_profile.in_id = ''
    profile.save()

最好的!

答案 1 :(得分:0)

我解决了这个问题:

SOCIAL_AUTH_DISCONNECT_PIPELINE = (
    'social.pipeline.disconnect.allowed_to_disconnect',
    'social.pipeline.disconnect.get_entries',
    'social.pipeline.disconnect.revoke_tokens',
    'social.pipeline.disconnect.disconnect',
    'profiles.utils.disconnect',
)

并将功能修改为:

def disconnect(strategy, entries, *args, **kwargs):
    backend = kwargs.get('name')

    profile=Profiles.objects.get(user=entries[0].user)
    if backend == 'facebook':
        profile.fb_id = False
    elif backend == 'twitter':
        profile.tw_id = False
    elif backend == 'instagram':
        new_profile.in_id = False
    profile.save()

现在它完美无缺。