我正在尝试创建一个注册视图,该视图既可以根据 POST 数据注册用户模型,也可以在响应中包括:
- 创建的用户数据(即
{'username': 'foo.bar', 'email': 'test@test.io'}
) - 令牌数据(即
{'expires_in': 36000, 'access_token': 'foo', 'scope': 'read write', 'token_type': 'Bearer', 'refresh_token': 'bar'}
)
因此,他们不必在注册后登录。我正在尝试使用这个views.py:
import json
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
import requests
from rest_framework import permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView
from accounts.serializers import UserSerializer, UserProfileSerializer
class UserProfile(APIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, format=None):
return Response()
def post(self, request, format=None):
user_profile = UserProfileSerializer(data=request.data) # data
if user_profile.is_valid():
user_profile.save() # returns User instance
# ask for a token
r = requests.post('http://myapp.com/auth/token',
data = {'client_id': '',
'client_secret': '',
'grant_type': 'password',
'username': user_profile.user.username,
'password': request.data['password']})
response_dict = json.loads(r.text)
response_dict['user_profile'] = user_profile.data
return Response(response_dict, status=status.HTTP_201_CREATED)
else:
return Response(user_profile._errors, status=status.HTTP_400_BAD_REQUEST)
但是我不知道我应该如何获得我的提供者client_id
和client_secret
(我创建了我的 oauth2_provider 应用程序)。
我应该把它当作 inmy_app = oauth2_provider.models.Application.objects.all()[0]
然后把它们当作my_app.client_id
andmy_app.client_secret
吗?有更好的方法吗?
另外,这有任何安全漏洞吗?
谢谢!