首页 文章

如何从django-rest-framework-social-oauth2转换令牌视图返回用户?

提问于
浏览
0

我目前正在使用Django-rest-framework-social-oauth2,它是convert-toke endpoints . 我将来自Facebook的用户令牌与请求发送到此终点,成功后,使用以下数据进行响应:

{
  "access_token": "************",
  "token_type": "Bearer",
  "expires_in": 36000,
  "refresh_token": "************",
  "scope": "read write"
}

但是,我也希望获得经过身份验证的用户的ID,所以我尝试在自定义视图中使用 request.user ,但它总是返回AnonymousUser .

class SocialView(ConvertTokenView):


 def post(self, request, *args, **kwargs):

        response = super(SocialView, self).post(request, *args, **kwargs)

EDIT:

我找到了一种方法,我可以通过响应中的access_token检索用户 .

user = AccessToken.objects.get(token=response.data['access_token'])

但我不确定这样做是否正确?

1 回答

相关问题