你好,我试图从数据库中获取社交令牌,但我得到了这个错误。
我找不到答案,我想尽了办法。
如果有人知道怎么解决这个问题,请告诉我。

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包模型的链接。
新的错误是:
属性错误/
类型对象“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

关于python - django获取社交 token 形式allauth,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30888487/

10-15 11:48