且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

谷歌登录姜戈睡觉框架+allauth+睡觉-auth

更新时间:2022-06-18 14:58:36

我还集成了djangorest framework+django-allauth+django-睡觉-auth+djangorest framework-jwt。 但是,我只是在Google上实现了登录,所以用户不能手动注册。

以下是我的代码:

views.py

from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from rest_auth.registration.views import SocialLoginView


class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    client_class = OAuth2Client

urls.py

...
path('auth/google', GoogleLogin.as_view(), name='google_login'),
...

settings.py

INSTALLED_APPS = [
    ...
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    ...
]
SITE_ID = 1
REST_USE_JWT = True    # this is for djangorestframework-jwt

[更新]
要在SocialLoginView终结点上使用codeparams:

  • 创建可访问的重定向URI(我认为它在前端,也可以使用简单视图)
  • 使用https回调(我在本地开发时使用ngrok)
  • 直接访问url(不要使用Google OAuth playgroud获取代码,因为client_id不同),或者直接复制OAuth palygroud上的请求url,更改redirect_uri和cliend_id。这是链接 https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<https_callback>&prompt=consent&response_type=code&client_id=<cliend_id>&scope=email&access_type=offline

这篇关于谷歌登录姜戈睡觉框架+allauth+睡觉-auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!