django社交身份验证与自定义用户模型不起作用

时间:2013-04-27 14:28:03

标签: django django-socialauth

我正在尝试在我的项目中实现django social auth,但我无法将其与我的自定义用户模型集成。每当我尝试登录到Facebook时,都会抛出错误并始终重定向到LOGIN_ERROR_URL页面。

我的settings.py文件如下所示。

FACEBOOK_APP_ID              = 'xxxxx'
FACEBOOK_API_SECRET          = 'xxxxx'
AUTHENTICATION_BACKENDS = (
   'social_auth.backends.facebook.FacebookBackend',
   'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_PIPELINE = (
  'social_auth.backends.pipeline.social.social_auth_user',
  'social_auth.backends.pipeline.associate.associate_by_email',
  'social_auth.backends.pipeline.misc.save_status_to_session',
  'social_auth.backends.pipeline.user.create_user',
  'social_auth.backends.pipeline.social.associate_user',
  'social_auth.backends.pipeline.social.load_extra_data',
  'social_auth.backends.pipeline.user.update_user_details',
  'social_auth.backends.pipeline.misc.save_status_to_session',
)

LOGIN_URL = '/user/login_register/'
LOGIN_ERROR_URL = '/user/login_register/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
SOCIAL_AUTH_FORCE_POST_DISCONNECT = True
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True

SOCIAL_AUTH_USER_MODEL = 'myapp.MyUser'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']

AUTH_USER_MODEL = 'myapp.MyUser'

我的自定义用户模型定义如下(仅使用电子邮件,不使用名字,姓氏或用户名)

class MyUserManager(BaseUserManager):
   def create_user(self, email, password=None):
      """
      Creates and saves a User with the given email and password.
      """
      if not email:
         raise ValueError('Users must have an email address')

      user = self.model(
          email=MyUserManager.normalize_email(email),
      )

      user.set_password(password)
      user.save(using=self._db)
      return user


class MyUser(AbstractBaseUser):

    email = models.EmailField(
        verbose_name='Email address',
        max_length=255,
        unique=True,
        db_index=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

现在,当我尝试使用facebook登录登录我的webapp时,它会将我带到facebook登录凭据页面,当我输入登录详细信息时,它会将我返回到LOGIN_ERROR_URL页面。在我输入facebook登录详细信息后,它没有在我的应用程序中创建新用户。

任何人都可以帮助解决我错过的问题吗?

编辑: -

我得到的错误是来自下方的AuthFailed(找到令牌的用户数据)错误消息 https://github.com/omab/django-social-auth/blob/master/social_auth/backends/facebook.py#L107

有关详细信息,我的login_resiter.html如下所示。

   <h4> Or Login Using </h4>
   <ul>
      <li><a rel="nofollow" href="{% url "socialauth_begin" "facebook" %}">facebook</a></li>
      <li><a rel="nofollow" href="{% url "socialauth_begin" "twitter" %}">twitter</a></li>
      <li><a rel="nofollow" href="{% url "socialauth_begin" "google-oauth2" %}">google</a></li>
      <li><a rel="nofollow" href="{% url "socialauth_begin" "yahoo" %}">yahoo</a></li>
   </ul>

   TEMPLATE_CONTEXT_PROCESSORS += (
     'social_auth.context_processors.social_auth_by_name_backends',
     'social_auth.context_processors.social_auth_login_redirect',
   )

由于

0 个答案:

没有答案
相关问题