django - OAuth2 : authenticate with email instead of username

标签 django oauth-2.0 django-rest-framework

我将 OAuth2 与 django-oauth-toolkit django-rest-framework 一起使用。

我通常通过以下方式对我的用户进行身份验证,以获取 token :

curl -X POST -d "grant_type=password&username=new_user&password=new_user" -u "GZwzDjPM89BceT8a6ypKGMbXnE4jWSzsyqbM3dlK:" http://localhost:8000/o/token/

有没有办法使用电子邮件而不是用户名来验证我的用户?

谢谢!

最佳答案

是的!可以通过将电子邮件设置为 username在用户模型中。

class User(AbstractBaseUser, PermissionsMixin):
 email =  models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,
 )
 first_name = models.CharField(max_length=30)
 last_name = models.CharField(max_length=30)

 USERNAME_FIELD = 'email'

那么电子邮件现在可以用作 username在请求中。
curl -X POST -d "grant_type=password&username=test@test.com&password=new_user" -u "GZwzDjPM89BceT8a6ypKGMbXnE4jWSzsyqbM3dlK:" http://localhost:8000/o/token/

关于django - OAuth2 : authenticate with email instead of username,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35661924/

相关文章:

python - Django Rest Framework - 具有 "."、 "-"、 "_"字符的字段用户名

python - 有什么官方方法可以获取模型的管理选项吗?

Django 信号 : receiver and proxy model?

java - SpringBoot OAuth2RestTemplate 重试请求

oauth-2.0 - 在门户中哪里可以获取我的 Azure AD B2C 发行人 URL

php - guzzle NULL响应lumen php oauth2

python - 按 MethodSerializer 排序

ruby-on-rails - 如何配置 apache 来运行 Django (mod_WSGI) 和 Rails (Passenger)

Django Postgres 错误,字段 "is not of a character type"

python - Django REST 框架 - 获取数据而不是链接