[英]Django UserSocialAuth matching query does not exist
I'm developing a web app with github API. 我正在使用github API开发一个Web应用程序。 I want to get the token of the user I have authenticated with Python Social Auth, but I get the following error
UserSocialAuth matching query does not exist
when I'm trying to access the token of the user. 我想获取已使用Python Social Auth进行身份验证的用户的令牌,但是在尝试访问用户的令牌时,出现以下错误
UserSocialAuth matching query does not exist
。
I really don't know what to do to access this extra data. 我真的不知道该怎么做才能访问这些额外的数据。
Here's my code when I'm trying to access the token : 这是我尝试访问令牌时的代码:
if request.user.is_authenticated:
gituser = request.user.social_auth.get(provider ='github-oauth2')
extra_data = str(gituser.extra_data['access_token'])
Thanks by advance ! 预先感谢!
You are trying to get single object with get() and since it does not exist, it returns DoesNotExist exception. 您正在尝试使用get()获取单个对象,由于该对象不存在,因此它返回DidNotExist异常。 Use filter instead of get as shown below:
使用filter代替get,如下所示:
gituser = request.user.social_auth.filter(provider ='github-oauth2')
for user in gituser:
extra_data = user.extra_data
Or you can use get_object_or_404 as shown below: 或者,您可以使用get_object_or_404,如下所示:
from django.shortcuts import get_object_or_404
gituser = get_object_or_404(UserSocialAuth, provider ='github-oauth2')
extra_data = gituser.extra_data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.