python-3.x – Django allauth how to get the user token from which the session is opened in the browser?

Question:

Good morning.

You need to post a picture on the vk wall on behalf of the user who logged in to the site.

Tried getting his token like this.

views.py
social_accounts = SocialAccount.objects.filter(provider='vk')
for social_account in social_accounts:
    socialtokens = SocialToken.objects.filter(id=social_account.id)

But I am not given a token of the one who logged in to the site, but the first one registered through the social network, and plus gives this token for use even to those who are not authorized.

I sort of figured out the unauthorized ones. Probably not correct, but they can no longer perform the cherished action.

 if request.user.is_authenticated:
        return render(request, 'parsing/Page1.html', {'values': ['Hello']})
    else:
        return render(request, 'parsing/Page2.html', {'values': ['не Hello']})

But it is impossible to get the user token from which the session is open. Where can I find a solution? I did not find this in the documentation on the off site. On the forums, too, I did not google anything like that.

Python3.6 Django2.0

Answer:

Well, it seems to me, first you need to find the correct account for the user from the session. Then, if this account exists, search for this account for its token.

social_account = SocialAccount.objects.filter(provider='vk',
                                              user=request.user.pk)
socialtoken = SocialToken.objects.filter(account=social_account.pk)

This is how it should work

social_account = SocialAccount.objects.get(provider='vk', user=request.user.pk)
social_token = SocialToken.objects.get(account=social_account)
Scroll to Top
AllEscort