Django Rest Framework:如何创建用于通过 Gmail 进行日志记录的端点 (Google)?
Django Rest Framework: How can I create endpoint for logging via Gmail (Google)?
我正在尝试创建 API,我想知道如何创建端点以通过 Gmail 帐户进行身份验证。好像 django-rest-auth
只支持 Facebook 和 Twitter。
有人可以给我提示吗?
我在我的一个项目中使用 Django rest auth,它支持 Google 登录。
它支持 django-allauth
支持的所有社交提供者。
Here 是 django-allauth 和 django-rest-auth
支持的社交提供者列表
# social_auth_view.py
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
# urls.py
urlpatterns += [
...,
url(r'^rest-auth/google/$', GoogleLogin.as_view(), name='google_login')
]
所以,如果你想支持任何其他社交提供者。
- 从
allauth.socialaccount.providers
. 为您的社交提供商导入适配器
- 创建新视图作为
rest_auth.registration.views.SocialLoginView
的子类。
- 将步骤 1 中导入的适配器添加到视图中作为
adapter_class
属性。
- 在 urls.py
中导入此视图
我正在尝试创建 API,我想知道如何创建端点以通过 Gmail 帐户进行身份验证。好像 django-rest-auth
只支持 Facebook 和 Twitter。
有人可以给我提示吗?
我在我的一个项目中使用 Django rest auth,它支持 Google 登录。
它支持 django-allauth
支持的所有社交提供者。
Here 是 django-allauth 和 django-rest-auth
# social_auth_view.py
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
# urls.py
urlpatterns += [
...,
url(r'^rest-auth/google/$', GoogleLogin.as_view(), name='google_login')
]
所以,如果你想支持任何其他社交提供者。
- 从
allauth.socialaccount.providers
. 为您的社交提供商导入适配器
- 创建新视图作为
rest_auth.registration.views.SocialLoginView
的子类。 - 将步骤 1 中导入的适配器添加到视图中作为
adapter_class
属性。 - 在 urls.py 中导入此视图