Get User ID from Django rest-auth Google login

Just wanted to post something that i got stuck in BADLY and couldnt find a way out. For someone who has worked with Django rest-auth social logins, They maybe aware that the response is always a key:
{
“key”: “6ee5ff4db218a25113f35580d00aeeaf2fe8a2d1”
}
But what if you need the User ID as well and not just the key? Well the answer to that what I found is to use this custom Class Based View.

class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
def post(self, request, *args, **kwargs):
response = super(GoogleLogin, self).post(request, *args, **kwargs)
token = Token.objects.get(key=response.data[‘key’])
return Response({‘token’: token.key, ‘id’: token.user_id})

Hopefully, This will prove useful to anyone who mayget stuck with Social logins like I did. Thank you

2 Likes