django-rest-auth google和facebook注册返回访问令牌和代码

时间:2020-01-23 12:32:37

标签: django django-rest-framework django-rest-auth

我正在使用Django构建rest api,并且使用Django rest auth进行社交身份验证。我相信我做对了所有事情。访问该路线后,我得到的答复是,我将同时为Google和Facebook提供访问令牌和代码。此时我迷路了。请有想法的人,请分享。

我已经从两家提供商那里获得了密钥和客户端ID,并将它们输入到我的settings.py和django admin中。

settings.py

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    ...
    'rest_auth',
    'rest_auth.registration',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.facebook',
    ...
]

SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'METHOD': 'oauth2',
        'SCOPE': ['email', 'public_profile', 'user_friends'],
        'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'INIT_PARAMS': {'cookie': True},
        'FIELDS': [
            'id',
            'email',
            'name',
            'first_name',
            'last_name',
            'verified',
            'locale',
            'timezone',
            'link',
            'gender',
            'updated_time',
        ],
        'EXCHANGE_TOKEN': True,
        'LOCALE_FUNC': 'path.to.callable',
        'VERIFIED_EMAIL': True,
        'VERSION': 'v2.12',
        'APP': {
            # get the key from "https://developers.facebook.com/apps/615248019004301/settings/basic/"
            'client_id': 'code',
            'secret': 'code',
            'key': ''
        }
    },
     'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'offline',
        },
        'APP': {
            # get from "console.developers.google.com/" then apis then credentials then oauthclient
            # fill in http://127.0.0.1:8000/accounts/google/login/callback/ in the “Authorized redirect URI” field
            'client_id': 'code.apps.googleusercontent.com',
            'secret': 'code',
            'key': ''
        }
    }
}

SITE_ID = 1

SOCIALACCOUNT_ADAPTER = "allauth.socialaccount.adapter.DefaultSocialAccountAdapter"

SOCIALACCOUNT_EMAIL_REQUIRED = ACCOUNT_EMAIL_REQUIRED

how my django admin is set up

The response i get

1 个答案:

答案 0 :(得分:0)

好的,我处于类似的情况,并且已经做了很多阅读。看来您已正确执行了所有设置。现在,您正尝试通过客户端的API执行注册/登录。

我将作一些假设,因为您在问题中没有提供太多详细信息。例如,我不确定您要访问的路线,或者您使用的是哪种API客户端(DRF可浏览API,React前端,移动应用程序)。就是说,使用哪种类型的客户端都没有关系,过程应该相同。

以下是步骤:

  • 您需要通过滚动自己的代码或使用适用于所使用技术的任何库来从API客户端启动注册/登录。例如,Facebook提供了自己的JavaScript SDK。如果您使用的是React,则可以使用react-facebook-login请注意,我个人没有使用过YMMV。
  • 例如,在Facebook上成功进行身份验证后,您将获得一个accessToken,客户端应将其发送到/rest-auth/facebook/上的API(或您在urls.py中配置的任何内容) 。
  • 发生这种情况时,应在后端创建一个新用户及其所有详细信息。

这是一个示例博客帖子,我发现作者在其中展示了一个React前端应用程序的流程:https://medium.com/@pratique/social-login-with-react-and-django-ii-39b8aa20cd27

这是我在SO上发现的类似流程的另一个示例。在答案中,用户使用的是python-social-auth软件包而不是django-allauth,但流程类似:https://stackoverflow.com/a/46476631/399435

由于您的客户端将向您的API发出请求,因此我相信您也必须正确设置CORS,这样的请求才能成功。

希望能为您指明正确的方向。我将很快自己尝试这种方法,如果我发现有所不同,我将返回编辑该答案。