django rest框架与jwt进行社交身份验证 [英] django rest framework social-auth with jwt

本文介绍了django rest框架与jwt进行社交身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi im正在使用jwt和angular 5开发SPA django rest框架。
我建立了登录和注册部分。现在,我正在寻找一种方法来添加社交登录名或注册到我的应用程序,并在深度搜索后获得jwt支持,我发现一些模块的文档非常不清楚(对我而言至少如此)。.该模块
https://github.com/st4lk/django-rest-social-auth 看起来不错
他说的用法是:

  / api / login / social / jwt_user / 

并且:

  / api / login / social / jwt / 

是jwt的终点,但是当我使用它们时我什么也没得到。 / p>

我们闲聊了一些,但是尚无定论。
任何人都可以向我和许多像我这样对这个问题感到困惑的开发人员解释解决方案?我们应该使用哪个模块,或者如果提到的模块可以使用

解决方案

OAuth 2.0工作流程具有rest-social -auth


  1. 前端需要了解每个社交服务提供商的以下参数:




    • client_id#仅在OAuth 2.0的情况下,社交服务提供商上已注册的应用程序的ID

    • redirect_uri#重定向到此URL的社交服务提供商将使用代码

    • scope =您的范围#例如电子邮件

    • response_type = code#所有oauth2.0提供程序都相同


  2. 用户确认。


  3. 社交提供程序使用参数代码重定向回redirect_uri。


  4. 前端现在可以登录用户了。为此,请发送带有提供者名称和代码的POST请求:


自述文件



这意味着您应该在 facebook开发人员<中设置一个应用程序(以Facebook为例) / a>,这是教程如何在Django中添加社交登录名,您可以在其中找到如何设置facebook应用程序和配置django_social。



在那之后,您将拥有client_id,redirect_uri您的Facebook应用程序。
下一步是获取参数代码。您可以通过对URL https://www.facebook.com/v2.5/dialog/oauth?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&display=popup&scope=email a>(scope = email,如果您想获取用户电子邮件)



如果一切正确,它将使用代码参数将您重定向到redirect_uri(例如: http:// YOUR_REDIRECT_URL /?code = 90159c983d3bdc28c0PPzfSQB8LI49BYlPA6Vs $ b b >因此,现在您有了代码,可以登录(注册)用户了。只需将带有提供者名称和代码的POST请求发送到url / api / login / social / jwt /

  { code: YOUR_CODE, provider: facebook} 

仅此而已,您将获得jwt令牌作为回应


hi im working on SPA django rest framework with jwt and angular 5 . i built login and sign up section. now im looking for a way to add social login or register to my app and have jwt support after deep search i found some module that have very unclear document(atleast to me).. this module https://github.com/st4lk/django-rest-social-auth looks ok he said use:

/api/login/social/jwt_user/

and:

/api/login/social/jwt/

are end point for jwt but this get me nothing when i'm using them..

we had some chat in slack but that was inconclusive anybody can explain solution to me and many developer like me that are confused about this issue ? which module should we use or if mentioned module is ok how should we use it

解决方案

There is OAuth 2.0 workflow with rest-social-auth

  1. Front-end need to know following params for each social provider:

    • client_id # only in case of OAuth 2.0, id of registered application on social service provider
    • redirect_uri # to this url social provider will redirect with code
    • scope=your_scope # for example email
    • response_type=code # same for all oauth2.0 providers
  2. User confirms.

  3. Social provider redirects back to redirect_uri with param code.

  4. Front-end now ready to login the user. To do it, send POST request with provider name and code:

README

it means, that you should set up an application (let's get a facebook as an example) in facebook developers, here is tutorial How to Add Social Login to Django where you can find how to set up facebook app and configurate django_social.

After that you will have client_id, redirect_uri for your facebook app. Next step is to get param code. You can get param code by making GET request to url https://www.facebook.com/v2.5/dialog/oauth?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&display=popup&scope=email (scope=email, if you want to get user email)

If everything is correct it will redirect you to your redirect_uri with code param (for example: http://YOUR_REDIRECT_URL/?code=90159c983d3bdc28c0PPzfSQB8LI49BYlPA6Vs)

So, now you have code, and you are ready to login (sign up) the user. Just send POST request with provider name and code to url /api/login/social/jwt/

{"code":"YOUR_CODE", "provider":"facebook"}

Thats all, you will get jwt token in response.

这篇关于django rest框架与jwt进行社交身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆