[英]How do I access the token generated using django's social auth?
I am using django's social_auth for facebook authentication. 我正在使用django的social_auth进行Facebook身份验证。 I am trying to access the token once a user is logged in using facebook so the app can do things like retrieve the user's friends and what is on his/her newsfeed. 用户尝试使用Facebook登录后,我正在尝试访问令牌,以便该应用程序可以执行诸如检索用户的朋友以及他/她的新闻源上的内容之类的操作。 However, I am having trouble getting that token. 但是,我在获取该令牌时遇到了麻烦。 Below is what I have. 以下是我所拥有的。 {{instance}} displays in the template but when I add {{token}} I get an error saying 'QuerySet' object has no attribute 'tokens'. {{instance}}显示在模板中,但是当我添加{{token}}时出现错误,提示“ QuerySet”对象没有属性“ tokens”。 How do I get the generated token? 如何获得生成的令牌?
def friends(request):
instance = UserSocialAuth.objects.filter(user=request.user).filter(provider='facebook')
token = instance.tokens
return render_to_response('reserve/templates/friends.html', {'token':token, 'instance':instance},
context_instance=RequestContext(request))
{{instance}}
<br>
{{token}}
The error says it all: a QuerySet
is a collection of whatever your searched for (in this case, UserSocialAuth
). 该错误说明了一切: QuerySet
是您要搜索的内容的集合(在本例中为UserSocialAuth
)。 To get a field in that model you need to iterate over the collection: 要在该模型中获取字段,您需要遍历集合:
tokens = [x.tokens for x in instance]
As an alternative, if you're sure your filter will return exactly one instance (not zero, not 2 or more) you can use get
instead of filter
: 作为替代方案,如果您确定过滤器将恰好返回一个实例(不是零,不是2或更多),则可以使用get
代替filter
:
instance = UserSocialAuth.objects.get(user=request.user, provider='facebook')
token = instance.tokens
Note however that a single provider can return more than one token , like oauth_token
and oauth_token_secret
, so you should select which one you want and/or format it appropriately. 但是请注意,单个提供程序可以返回多个令牌 ,例如oauth_token
和oauth_token_secret
,因此您应该选择所需的令牌和/或对其进行适当的格式化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.