0

I'm developing a web app with github API. I want to get the token of the user I have authenticated with Python Social Auth, but I get the following error UserSocialAuth matching query does not exist when I'm trying to access the token of the user.

I really don't know what to do to access this extra data.

Here's my code when I'm trying to access the token :

    if request.user.is_authenticated:
        gituser = request.user.social_auth.get(provider ='github-oauth2')
        extra_data = str(gituser.extra_data['access_token'])

Thanks by advance !

Josh21
  • 506
  • 5
  • 15

1 Answers1

1

You are trying to get single object with get() and since it does not exist, it returns DoesNotExist exception. Use filter instead of get as shown below:

gituser = request.user.social_auth.filter(provider ='github-oauth2')

for user in gituser:
  extra_data = user.extra_data

Or you can use get_object_or_404 as shown below:

from django.shortcuts import get_object_or_404

gituser = get_object_or_404(UserSocialAuth, provider ='github-oauth2')
extra_data = gituser.extra_data
user8193706
  • 2,387
  • 2
  • 8
  • 12
  • Thanks for your answer ! But now it gives me " 'QuerySet' object has no attribute 'extra_data'" – Clément Habib May 17 '19 at 09:03
  • 1
    You can't get the extra_data directly. When using filter it returns queryset so loop through the queryset.See I updated my answer.In that way you can get extra_data – user8193706 May 17 '19 at 09:39
  • But get_object_or_404 will return only one object. For multiple you can use get_list_or_404 – user8193706 May 17 '19 at 09:47