简体   繁体   中英

Django request.user is empty

Using django, I am authenticating the user through Google. I get the initial request tokens & redirect the user to google for auth. After which google redirects the user back to my website (using the redirect_url I provide).

At this point the request.user.id is None so is request.user.username why is this happening? I need the user ID to enter the access_tokens (that google sends me) into the DB.

Under what conditions can request.user object in Django be empty?

UPDATE1: When I get redirected back from Google with the url pattern as http://mywebsite.com/lserv?s=goog control comes back to my django views function, but django gives me the request.user object user as Anonymous user with no username or id. why?

UPDATE2: all this is running on python manage.py runserver for now...

UPDATE3: Anybody faced anythn similar to this? basically, out of no reason the user in request clears out automatically & I get assigned as Anonymous user. All this happens between url requests from the user (from browser). Why?

If you are using oauth api from google. To get the user you have to do something like this

from google.appengine.api import oauth

# ...
        try:
            # Get the db.User that represents the user on whose behalf the
            # consumer is making this request.
            user = oauth.get_current_user()

        except oauth.OAuthRequestError, e:
            # The request was not a valid OAuth request.
            # ...

Django's auth mechanism has nothing to do with Google's or any other auth service. If you want to integrate third party auth service with your Django site, you should do it youself.

If you're using oauth2(https://github.com/simplegeo/python-oauth2) library, it's README(https://github.com/simplegeo/python-oauth2/blob/master/README.md) has a section named "Logging into Django w/ Twitter" may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM