Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django get the social token form allauth

Hello i tried to get the social token from the DB but i get this error. I can't find any answer to this and tried everything i can think of.

Please if anyone knows how to fix this.

NameError at /
name 'user' is not defined
Request Method: GET
Request URL:    http://localhost:3000/
Django Version: 1.8.2
Exception Type: NameError
Exception Value:    
name 'user' is not defined
Exception Location: /home/dk/user-new/just/fb/views.py in home, line 14
Python Executable:  /home/dk/user-new/bin/python
Python Version: 3.4.0
Python Path:    
['/home/dk/user-new/just',
 '/home/dk/user-new/lib/python3.4',
 '/home/dk/user-new/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/dk/user-new/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/dk/user-new/lib/python3.4/site-packages']
Server time:    Wed, 17 Jun 2015 10:09:50 +0000






from django.shortcuts import render
import requests
from allauth.socialaccount import providers
from allauth.socialaccount.models import SocialLogin, SocialToken, SocialApp
from allauth.socialaccount.providers.facebook.views import fb_complete_login
from allauth.socialaccount.helpers import complete_social_login
import allauth.account


def home(request):

    access_token = SocialToken.objects.filter(user=user, account__provider='facebook')

Update:

I added a link to the allauth package model.

The new error is:

AttributeError at / type object 'SocialLogin' has no attribute 'account'

def home(request):
    #user = request.user

        user = SocialLogin.account.user
        access_token = SocialToken.objects.filter(account=user,account__provider='facebook') 

https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py

Update:

Can't convert 'QuerySet' object to str implicitly

access_token = SocialToken.objects.filter(account__user=request.user, account__provider='facebook') 
r = requests.get('https://graph.facebook.com/me?access_token='+access_token+'&fields=id,name,email') #MY_CORRECT_TOKEN&fields=id,name,email
like image 783
dk1990 Avatar asked Jan 09 '23 03:01

dk1990


1 Answers

you didn't define user variable before using it

def home(request):
    #user = request.user
    access_token = SocialToken.objects.get(account__user=request.user, account__provider='facebook') #get instead of filter (you need only one object)

    r = requests.get('https://graph.facebook.com/me?access_token='+access_token.token+'&fields=id,name,email') #add access_token.token to your request
like image 80
Amr Abdelaziz Avatar answered Jan 10 '23 16:01

Amr Abdelaziz