简体   繁体   English

如何访问使用django的社交身份验证生成的令牌?

[英]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? 如何获得生成的令牌?

view 视图

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

template 模板

{{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_tokenoauth_token_secret ,因此您应该选择所需的令牌和/或对其进行适当的格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python-social-auth 和 django-graphql-auth 返回刷新令牌? - How can I return the refresh token using python-social-auth and django-graphql-auth? 如何通过 django-social-auth 使用 django 访问用户信息? - How do I access user information with django with django-social-auth? 如何使用social-auth-app-django刷新令牌? - How can I refresh the token with social-auth-app-django? 如何使用python-social-auth在代码中通过Access Token进行身份验证 - How to authenticate by Access Token in code using python-social-auth Django中的Python社交身份验证可使用应用程序访问令牌将URL发布到我的业务Facebook页面 - Python social auth in django to post URL to my business Facebook page using application access token 使用django-social-auth(omab)自动刷新访问令牌 - Automatically refresh access token with django-social-auth (omab) social-auth-app-django:刷新 access_token - social-auth-app-django: Refresh access_token 在Django中使用Python社交身份验证令牌身份验证时出现ValueError? - ValueError while using Python Social Auth token authentication in Django? 使用python social auth通过访问令牌注册用户 - Using python social auth to register users by access token 如何使用 django-graphql-social-auth 库获取刷新令牌 - How to get the Refresh Token with the django-graphql-social-auth library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM