In django allauth, how do I get a username from an OpenId / Google login?

I saw a couple of similar questions, but they did not seem to answer the problem I am having.

So far, I have only performed manage.py runserver , working locally.

At first I tried to just put this in my .py settings:

 import allauth ACCOUNT_EMAIL_REQUIRED = True 

then tried to expand it to:

 import allauth ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_AUTHENTICATION_METHOD = "username_email" ACCOUNT_USERNAME_REQUIRED = True SOCIALACCOUNT_AUTO_SIGNUP = True 

but when I try to log in with my google account (that is, openId-googlem is not direct Google), I see that it gets my email address, but it does not use my email address. In the callback function, I tried to get into a bunch of print statements, and it looked like it was extracting this from the return URL when I type user.__dict__ :

 {'_state': <django.db.models.base.ModelState object at 0x101604dd0>, 'date_joined': datetime.datetime(2012, 12, 3, 4, 35, 41, 949526, tzinfo=<UTC>), 'email': u' CORRECT_USERNAME@gmail.com ', 'first_name': '', 'id': None, 'is_active': True, 'is_staff': False, 'is_superuser': False, 'last_login': datetime.datetime(2012, 12, 3, 4, 35, 41, 949506, tzinfo=<UTC>), 'last_name': '', 'password': '', 'username': ''} 

That is, it fills in the email field, but not the username field. I thought by default allauth would fill in username with CORRECT_USERNAME , but instead it looks empty. Then, when I print the account, I see nothing in the user_id field.

When I bounce to the profile page, it lists username as β€œuser”, which looks like an absolute reserve / default value. That is, it is:

 {% user_display user %} <p>Another try... <br /> {{user.username}} <p>---------------------------</p> {{ user.account_provider }} <p>---------------------------</p> {{ user.get_provider }} <p>---------------------------</p> 

gives me the following:

  user Another try... user --------------------------- --------------------------- --------------------------- 

I do not see errors in the server console.

Is there anything else I need to add to settings.py? Is there a callback function that I need to write in order to extract the username from the email field?

I am using python 2.6.1 (Snow Leopard) if that matters.

Thanks in advance!

+4
source share
1 answer

If you look at the OpenID code django-allauth, you will see that it does not retrieve the username, but only the email address, first_name, last_name and name:

https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/providers/openid/provider.py#L65

As for the parameter ACCOUNT_USERNAME_REQUIRED, you can see in the documents that it is used in the registration form:

ACCOUNT_USERNAME_REQUIRED (= True) The user must enter a username at registration. Please note that the user will be prompted to do this even if an email message is set for ACCOUNT_AUTHENTICATION_METHOD. Set to False if you do not want to prompt the user to enter a username.

I hope this helps you in your doubts.

0
source

All Articles