django oauth2身份验证,带有client_id和client_Secret

时间:2018-12-18 13:47:29

标签: django oauth-2.0 django-rest-framework

我在Django中实现了Oauth2,并且刷新令牌位于o / token / url下, 我想定义另一个这样的网址:

path('api / v1 / login',Login.as_view()), 在我的登录视图中,我想要这样的东西:

class login(APIView):
  def post(self,request):
    client_id = "123"
    client_Secret = "123"
    username = request.query_params.get('username')
    ....
   *problem is here*

我想在登录类中定义这些参数,然后将其传递给o / token / url并获得令牌。 实际上,当用户输入www.example.com/api/v1/login地址时,它仅输入用户名和密码,并且之前在我的代码中我对OAuth说了我的客户信息是什么,然后令牌就会生成。

1 个答案:

答案 0 :(得分:0)

我认为您想要做的是为您的用户定义一个登录路由,的确是,如果我们在前端传递client_id和client_secret,将会有很多安全问题,因此我们将其隐藏在代码中。 您应该做的是为登录定义一个新的路由,然后在视图中定义一个需要用户/用户通过的post方法,并将此数据与您在代码中输入的一些数据一起发送到请求命令(请在此处检查: enter link description here) 请注意,为了在输出中具有JSON响应,您需要返回Response(r.json()) 从而: url.py

class Login(APIView):
def post(self, request, *args, **kwargs):
    username = request.POST['username']
    password = request.POST['password']
    r = requests.post('http://localhost:8000/api/o/token/', #your token address
                      data = {'grant_type':'password', # your defined grant type
                              'client_id':'123', # your clinet id
                              'client_secret':'123', #your client secret
                              'username': username, # your username that you get from user
                              'password':password #your password that you get from user

view.py

{{1}}

})         返回json格式的Response(r.json())#response

相关问题