简体   繁体   English

Apple 使用 allauth 和 rest-auth 在 django rest 框架中登录

[英]Apple login in django rest framework with allauth and rest-auth

I have implemented Apple login in django with allauth and rest-auth.我已经在 django 中使用 allauth 和 rest-auth 实现了 Apple 登录。 I implemented same way as Google login which worked perfectly.我实现了与谷歌登录相同的方式,效果很好。

views.py视图.py

class AppleLogin(SocialLoginView):
    adapter_class = AppleOAuth2Adapter

urls.py网址.py

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

pip versions点子版本

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

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当我如下测试时,我收到KeyError: 'id_token'这就是错误的来源: https : //github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/providers/apple/views。 py#L92

I have no idea how to fix this error.我不知道如何解决这个错误。

Thank you for your help !感谢您的帮助 !

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'

Use this custom serializerClass.使用此自定义序列化器类。 https://github.com/pennersr/django-allauth/pull/2424#issuecomment-651913243 https://github.com/pennersr/django-allauth/pull/2424#issuecomment-651913243

It seems that the problem is in django-rest-auth Your authentication view should look like this似乎问题出在 django-rest-auth 你的身份验证视图应该是这样的

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

The only change in the SerializerClass is in the validate function so you can just override that method SerializerClass 中唯一的变化是在validate函数中,因此您可以覆盖该方法

from rest_auth.registration.serializers import SocialLoginSerializer

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django rest-auth / allauth和微博集成 - Django rest-auth/allauth and weibo integration Postman 和 django rest 框架(rest-auth) - Postman and django rest framework (rest-auth) django-allauth和rest-auth Facebook登录令牌不适用于JWT身份验证 - django - allauth and rest-auth facebook login token does not work with JWT authentication 在 django allauth 中解决未授权 /rest-auth/registration/ 错误 - solving error unauthorized /rest-auth/registration/ in django allauth 如何在Django Framework中将自定义响应返回给api / rest-auth / login / - How to return customized response to api /rest-auth/login/ in Django Framework django rest-auth login token 使用方法 - django rest-auth login token use approach 如何使用 allauth 和 rest-auth 从 React 前端在 Django 中重新发送确认电子邮件 - How to resend confirmation email in Django from a React front end, using allauth and rest-auth Django rest-auth allauth 注册 email,名字和姓氏,没有用户名 - Django rest-auth allauth registration with email, first and last name, and without username django:Facebook注册API的rest-auth和allauth需要CSRF令牌 - django: rest-auth and allauth for Facebook registration API requires CSRF token 用于帐户注册和登录的Django allauth和rest框架 - Django allauth and rest framework for account signup and login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM