如何在python-social-auth django中设置额外数据?

时间:2013-11-16 22:49:02

标签: python django socialauth

我有一个问题,当尝试使用管道保存额外数据但它们没有保存。 这是一个代码:

def save_picture(strategy, user, response, details,social_user,
            is_new=False, *args, **kwargs):

if is_new and strategy.backend.name == 'facebook':
    url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
social_user.set_extra_data('url':url)

也许我必须使用不同的方法?

我找到了django-social-auth的解决方案,但它们不能与python-social-auth一起使用:

social_user.extra_data['your_field']

但它们不起作用,我尝试使用

social_user.set_extra_data({'url': url})

但这引起了例外:

Environment:


Request Method: GET Request URL: http://friends-on-map.herokuapp.com/complete/facebook/?redirect_state=6Vnf0DO7qEL4uEbK1bxubv9mlYF0pZgZ&code=AQAmR8F-dhTnjVYvsJ1zfnWIE_qJOfsF-hbJmJ95bC-gSFnbgpAHCaA-gPsYoa3eO-83Bk4u32E2dISKZ5jsRfQ49RbmrvzbpQFkwn886h7nqf3Eol4sKuqei7Zjtvtzq83X7tl6jHaN5tVcGsuhFbGZGBZPmwtaYpz2viNN-WcIRaj1WMmns_SpI7F4npYZZYHu_gkM1tyOnYkyupJE0hVEggkxJKi9d5Zx9J58FCp90K0IUHZbdgr63Rn2rUNtYsePqb85xVUfVolZloc2kDRySL_SUBlbT7cGKI633BcEQ1lsXdfD4s5e-2XeqXRj40w&state=6Vnf0DO7qEL4uEbK1bxubv9mlYF0pZgZ

Django Version: 1.5.5 Python Version: 2.7.4 Installed Applications: ('django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.sites',  'django.contrib.messages',  'django.contrib.staticfiles',  'django.contrib.admin',  'gunicorn',  'south',  'social.apps.django_app.default',  'auth') Installed Middleware: ('django.middleware.common.CommonMiddleware',  'django.contrib.sessions.middleware.SessionMiddleware',  'django.middleware.locale.LocaleMiddleware',  'django.middleware.csrf.CsrfViewMiddleware',  'django.contrib.auth.middleware.AuthenticationMiddleware',  'django.contrib.messages.middleware.MessageMiddleware')


Traceback: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/utils.py" in wrapper
  32.             return func(request, backend, *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/views.py" in complete
  25.                        redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/actions.py" in do_complete
  54.                                  *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/base.py" in complete
  69.         return self.backend.auth_complete(*args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py" in auth_complete
  74.         return self.do_auth(access_token, response, *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py" in do_auth
  106.         return self.strategy.authenticate(*args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/django_strategy.py" in authenticate
  69.         return authenticate(*args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
  60.             user = backend.authenticate(**credentials) File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py" in authenticate
  78.         return self.pipeline(pipeline, *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py" in pipeline
  81.         out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py" in run_pipeline
  106.             func = module_member(name) File "/app/.heroku/python/lib/python2.7/site-packages/social/utils.py" in module_member
  22.     return getattr(module, member)

Exception Type: AttributeError at /complete/facebook/ Exception Value: 'module' object has no attribute 'load_extra_data'

1 个答案:

答案 0 :(得分:4)

我解决了它,在代码下面做了伎俩。

def get_user_avatar(strategy, details, response, uid, user, *args, **kwargs):
    social = kwargs.get('social') or strategy.storage.user.get_social_auth(
        strategy.backend.name,
        uid
    )
    url = None
    if strategy.backend.name == 'facebook':
        url = "http://graph.facebook.com/%s/picture?type=large" % response['id']

    if url:
        social.set_extra_data({'photo': url})