3

我正在使用 DRF 作为休息框架,并希望使用社交登录来进行用户注册和登录。目前,我正在使用 Angular 向 Google 注册,这很成功并得到了以下响应:

"id":"105372307735.........",
"name":"XYZ",
"email":"xyz@gmail.com",
"photoUrl":"https://lh4.googleusercontent.com/-C..............",
"authToken":"ya29.Glx2.....................",
"provider":"GOOGLE"

我如何使用相同的响应在 django rest 框架中注册用户和登录?

谢谢

4

1 回答 1

1

我知道为时已晚,但这可能会帮助那些在社交登录方面苦苦挣扎的人,因此将其发布在这里。使用这个模块django-rest-framework-social-oauth2,按照官方 Github 上的说明进行操作,他们有 Facebook 和 Google 的示例。

此外,将这些管道添加到您的设置中

SOCIAL_AUTH_PIPELINE = (
  'social_core.pipeline.social_auth.social_details',
  'social_core.pipeline.social_auth.social_uid',
  'social_core.pipeline.social_auth.auth_allowed',
  'social_core.pipeline.social_auth.social_user',
  'social_core.pipeline.user.get_username',
  'social_core.pipeline.social_auth.associate_by_email',
  'social_core.pipeline.user.create_user',
  'social_core.pipeline.social_auth.associate_user',
  'social_core.pipeline.social_auth.load_extra_data',
  'social_core.pipeline.user.user_details',
)

这将允许用户使用单个用户帐户的多个社交登录来验证自己的身份。

python manage.py makemigrations

python manage.py migrate

如果您已正确按照他们的官方 GitHub 上的说明进行操作,那么您应该能够访问 localhost:8000/auth/convert-token 从那里您可以将您的令牌转换为您的应用程序令牌并使用它来验证用户身份。

于 2020-09-26T15:09:43.740 回答