第三方重定向后,Django request.user成为AnonymousUser [英] Django request.user become AnonymousUser after third party redirect

查看:84
本文介绍了第三方重定向后,Django request.user成为AnonymousUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test.html:

test.html:

<a href="https://auth.ebay.com/oauth2/authorize?    ...">authorize</a>

views.py:

from django.contrib.auth.decorators import login_required

@login_required
def myview(req):
    user = req.user

   return render(req, 'test.html')

对于ebay的oauth流程,您必须为用户提供指向ebay服务器的链接,该链接询问用户是否要向您提供凭据.如果他们接受,则ebay会将用户重定向到具有包含访问密钥的查询字符串的给定URL.

For ebay's oauth process, you have to provide users with a link to ebay's server, which asks the user if they want to give credentials to you. If they accept, ebay redirects the user to a given url with a querystring containing the access key.

问题是,当我使用ebay授权我的应用程序时,用户将被重定向到我的登录页面(尽管已经登录).如果删除 @login_required 装饰器,则 req.user 返回AnonymousUser.这是一个问题,因为我不知道将访问令牌分配给哪个用户.

The problem is, when I authorize my app with ebay, the user gets redirected to my login page (despite already being logged in). If I remove the @login_required decorator, req.user returns AnonymousUser instead. This is a problem since I don't know which user to assign the access token to.

我在这里想念什么?

请注意,我正在使用ngrok来隧道服务器,并且没有问题
呈现 myview ,而不是用户是匿名用户.

Note that I am using ngrok to tunnel my server, and I have no problems
rendering myview other than the fact that the user is Anonymous.

推荐答案

问题是,当我最初登录用户时,我使用的是域 localhost:8000 而不是我的ngrok实例.

The problem was that when I logged the user in initially, I was using the domain localhost:8000 instead of my ngrok instance.

使用我的ngrok地址登录用户即可解决此问题.

Logging my user in using my ngrok address fixed the problem.

这篇关于第三方重定向后,Django request.user成为AnonymousUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆