Django request.user become AnonymousUser after third party redirect

故事扮演 提交于 2021-01-27 12:27:54

问题


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

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.

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.

What am I missing here?

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.


回答1:


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

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



来源:https://stackoverflow.com/questions/57845566/django-request-user-become-anonymoususer-after-third-party-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!