【发布时间】:2019-04-02 18:57:19
【问题描述】:
当我通过用户名/密码进行 JWT 身份验证时,它的工作原理是这样的:
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
'''post looks like: <QueryDict: {
'csrfmiddlewaretoken': ['Pd1mjNUXEdeiObEGOg8oNeqU18nwMVkSu8C29e0POGKwa2kY3yHiXk6hOEzuatMg'],
'email': ['tom@gmail.com'],
'password': ['xyxyzuzu'],
'evaluateUsername': ['Login']
}>'''
tokens = MyTokenObtainPairSerializer().validate(request.POST)
request.session["accessToken"] = tokens["access"]
request.session["refreshToken"] = tokens["refresh"]
很简单。但是,如果我使用的是 Google 登录,我将无法访问用户的电子邮件或密码。我可以通过以下方式轻松获取电子邮件:
if not request.POST.get('email'):
request.POST._mutable = True
request.POST['email'] = user.email
# request.POST['password'] = '********' # placeholder
request.POST._mutable = False
但它仍然会在 django authenticate(user, password) 上失败,所以我收到以下错误:
rest_framework.exceptions.ValidationError: [ErrorDetail(string='No active account found with the given credentials', code='invalid')]
我该如何解决这个问题,因为如果用户使用 Google Auth 登录,我真的不知道用户的密码,并且任何提供虚假值的方式都会导致错误?
【问题讨论】:
标签: python django python-3.x django-rest-framework jwt