【问题标题】:Django Social Auth - Not Enough Values To Unpack When Trying To Send EmailDjango Social Auth - 尝试发送电子邮件时没有足够的值来解压
【发布时间】:2018-06-28 07:05:16
【问题描述】:

我正在使用 Django-Python Social Auth 应用程序,并且我正在尝试扩展我的管道,以便当用户使用 Facebook 注册时,他们会同时注册到我的 Mailchimp 邮件列表(并发送一封电子邮件以验证)。如果用户使用传统输入法(但不是通过社交身份验证)注册,则此方法可以正常工作。并且返回此错误:

ValueError at /oauth/complete/facebook/ not enough values to unpack (expected 2, got 1)

这里是完整的堆栈跟踪:

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_django\utils.py" in wrapper
  49.             return func(request, backend, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_django\views.py" in complete
  33.                        *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\actions.py" in do_complete
  41.         user = backend.complete(user=user, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\base.py" in complete
  40.         return self.auth_complete(*args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\utils.py" in wrapper
  252.             return func(*args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\facebook.py" in auth_complete
  111.         return self.do_auth(access_token, response, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\facebook.py" in do_auth
  153.         return self.strategy.authenticate(*args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_django\strategy.py" in authenticate
  107.         return authenticate(*args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py" in authenticate
  70.             user = _authenticate_with_backend(backend, backend_path, request, credentials)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py" in _authenticate_with_backend
  116.     return backend.authenticate(*args, **credentials)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\base.py" in authenticate
  80.         return self.pipeline(pipeline, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\base.py" in pipeline
  83.         out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\backends\base.py" in run_pipeline
  112.             func = module_member(name)

File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\social_core\utils.py" in module_member
  57.     mod, member = name.rsplit('.', 1)

Exception Type: ValueError at /oauth/complete/facebook/
Exception Value: not enough values to unpack (expected 2, got 1)

我的管道如下所示:

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_by_email',  # <--- enable this one
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'sendsub',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

您可以看到我添加了一个基于此函数的sendsub 函数——并且在某种程度上模仿了off this page here

def sendsub(backend, user, response, *args, **kwargs):
    profile = user.get_profile()
    email = profile.email
    thread = threading.Thread(target=self.run, args=())
    thread.daemon = True                     
    thread.start()                                 

    def run(self):
        API_KEY = settings.MAILCHIMP_API_KEY
        LIST_ID = settings.MAILCHIMP_SUBSCRIBE_LIST_ID
        api = mailchimp.Mailchimp(API_KEY)
        try:
            api.lists.subscribe(LIST_ID, {'email': self.email})
        except:
            return False

当这个sendsub 函数应用到我的常规注册页面一个用户模型时再次正常工作..这里是函数(略有不同)。

class SendSubscribeMail(object):
    def __init__(self, email):
        self.email = email
        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                     
        thread.start()                                 

    def run(self):
        API_KEY = settings.MAILCHIMP_API_KEY
        LIST_ID = settings.MAILCHIMP_SUBSCRIBE_LIST_ID
        api = mailchimp.Mailchimp(API_KEY)
        try:
            api.lists.subscribe(LIST_ID, {'email': self.email})
        except:
            return False

【问题讨论】:

  • 您的问题中缺少一条重要信息。完整的堆栈跟踪
  • 好的,稍后会添加一秒钟--谢谢
  • 全部更新,出现完整错误。谢谢

标签: python django python-social-auth django-socialauth


【解决方案1】:

SOCIAL_AUTH_PIPELINE 中的条目应该是导入路径的形式,在这种情况下会导致错误,因为sendsub 不是正确的导入路径,您需要指定它定义的模块,如module.sendsub。

为了验证导入路径是否正确,您应该能够在 django shell 中以import module.sendsub 的形式运行它,并且应该成功导入它。

【讨论】:

  • 迟来的感谢您的评论!所以,如果理解正确,我需要将我的代码放在一个模块中。我用 utils 文件和 init 文件创建了一个 utils 文件夹。在 shell 中,如果我执行导入 utils.utils.sendsub,我会得到一个“没有名为 .. 的模块,而 utils.utils 不是一个包……呃,忘记了其中的一些东西---
  • 抱歉混淆了模块---与包...我将 utils 文件放在我的根文件夹中,并且似乎正在工作。谢谢!
【解决方案2】:

希望这还不算太晚。但是在settings.py中,请确保AUTHENTICATION_BACKENDS后面有逗号,否则会被当作字符串,从而导致错误。

【讨论】:

    【解决方案3】:

    好的,错误本身来自社交身份验证。但那是因为你没有给它正确的数据。

    mod, member = name.rsplit('.', 1)
    

    它试图将名称分成两部分,名称中没有点,因此您无法从中得到两个部分。所以不可能分配给mod和member。现在我们知道了问题所在,但我们还有很长的路要走去解决它。第一步是找出罪魁祸首。我会说它就在这里。

    thread = threading.Thread(target=self.run, args=())
    thread.daemon = True                     
    thread.start()   
    

    在网络应用程序中创建自己的线程是一件非常危险的事情。您应该同步执行此任务,或者您应该委托给一个任务队列并让它完全异步发生。

    【讨论】:

    • 感谢您的帮助。我正在关注本教程 --djangopy.org/package-of-week/…,老实说,我对线程一无所知,学习这一点很有趣。现在,我有单独的功能可以将用户添加到 mailchimp 列表(一个用于常规注册,另一个用于通过社交身份验证)。我认为最好在 django 中进行任何新注册时运行任务以将用户添加到 mailchimp 列表。对此有何建议?有没有办法检查是否异步添加了新用户?还是这是个坏主意?
    • 我认为目前您可能应该专注于同步完成它。一旦你对框架更加熟悉了,你就可以探索 celery
    • 啊……是的,一直在研究芹菜。我只是在我的用户模型中添加了一个 post_save 信号,将一个用户添加到 mailchimp 列表中。它似乎适用于我的所有注册(普通 + facebook),看到这样做有什么问题吗? @receiver(post_save, sender=User) def send_sub(sender, **kwargs): if kwargs.get('created', True): email = kwargs.get('instance').email SendSubscribeMail(email)
    • nopes 使用信号是一种非常标准且有用的方式。不错的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多