Apple在Django Rest框架中使用allauth和rest-auth登录 [英] Apple login in django rest framework with allauth and rest-auth

查看:92
本文介绍了Apple在Django Rest框架中使用allauth和rest-auth登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用allauth和rest-auth在django中实现了Apple登录.我实现了与Google登录相同的方式,效果很好.

I have implemented Apple login in django with allauth and rest-auth. I implemented same way as Google login which worked perfectly.

views.py

class AppleLogin(SocialLoginView):
    adapter_class = AppleOAuth2Adapter

urls.py

urlpatterns = [
    path("auth/apple/", AppleLogin.as_view(), name="apple-login"),
]

主要版本

Django==2.2.17
django-allauth==0.43.0
django-rest-auth==0.9.3
djangorestframework==3.8.2
djangorestframework-jwt==1.11.0

当我进行如下测试时,我得到 KeyError:'id_token',这就是错误的出处:

When I test as below I'm getting KeyError: 'id_token' and this is where error comes from: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/providers/apple/views.py#L92

我不知道如何解决此错误.

I have no idea how to fix this error.

谢谢您的帮助!

curl -X POST 'https://.../auth/apple/' \
              -d 'access_token=AUTHENTICATION_CODE'

or

curl -X POST 'https://.../auth/apple/' \
              -d 'id_token=ID_TOKEN'   \
              -d 'access_token=AUTHENTICATION_CODE'

推荐答案

使用此自定义的serializerClass. https://github.com/pennersr/django-allauth/pull/2424#issuecomment-651913243

Use this custom serializerClass. https://github.com/pennersr/django-allauth/pull/2424#issuecomment-651913243

问题似乎出在django-rest-auth您的身份验证视图应如下所示

It seems that the problem is in django-rest-auth Your authentication view should look like this

from allauth.socialaccount.providers.apple.views import AppleOAuth2Adapter
from allauth.socialaccount.providers.apple.client import AppleOAuth2Client
from rest_auth.registration.views import SocialLoginView

class AppleLogin(SocialLoginView):
    adapter_class = AppleOAuth2Adapter
    callback_url = 'https://anycallbackurlhere'
    client_class = AppleOAuth2Client
    serializer_class = CustomAppleSocialLoginSerializer

SerializerClass中唯一的更改是在 validate 函数中,因此您可以覆盖该方法

The only change in the SerializerClass is in the validate function so you can just override that method

from rest_auth.registration.serializers import SocialLoginSerializer

class CustomAppleSocialLoginSerializer(SocialLoginSerializer):
    def validate(self, attrs):
        .... #copy the method from the link above

这篇关于Apple在Django Rest框架中使用allauth和rest-auth登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆