python – django获得社交令牌形式allauth

您好我试图从数据库中获取社交令牌,但我收到此错误.

我找不到任何答案,并尝试了我能想到的一切.

如果有人知道如何解决这个问题.

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')

更新:

我添加了allauth包模型的链接.

新错误是:

/的AttributeError
类型对象’SocialLogin’没有属性’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

更新:

无法隐式地将’QuerySet’对象转换为str

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

最佳答案 在使用之前没有定义用户变量

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
点赞