【问题标题】:Python Social Auth, Extending Disconnect PipelinePython Social Auth,扩展断开管道
【发布时间】:2015-06-25 22:52:30
【问题描述】:

我有一个用户配置文件表,它有 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,任何帮助将不胜感激。

【问题讨论】:

    标签: python django python-social-auth


    【解决方案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()
    

    最好的!

    【讨论】:

    • 您好,感谢您的回答。我尝试了您的解决方案,但出现了一个新错误,它说:列表索引超出范围
    • 如果您使用社交身份验证自己的断开连接并从 args 中删除 user_storage 并在函数中阻止,则此解决方案有效。
    【解决方案2】:

    我解决了这个问题:

    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()
    

    现在可以完美运行了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-02
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2015-10-25
      • 2015-02-24
      相关资源
      最近更新 更多