I was addicted to not being able to get an email address from google with django-allauth authentication

When implementing the social login function using django-allauth, I was addicted to not being able to get email information. Since it is a big deal, I will leave the event, cause, and countermeasures in this article.

What happened

What I was trying to do was to use django-allauth to enable google social login. I confirmed that I could log in by implementing it as described in the official document and qiita article, but when I looked at the created account information, for some reason the email address was empty. not_email.png Figure: Contents of the created account after google social login (check on the management screen)

Cause

In conclusion, the cause was that the scope of the google provider was not set properly. The google provider that django-allauth has by default sets the default scope to profile only. The email address information does not exist in the response from the `profileʻ API. Therefore, since the information was not obtained from google in the first place, it means that this event occurred.

So how to deal with it ... The solution is to add an "API that gives you email address information" to the scope. By the way, in the case of google, the name of the API is ʻemail`.

*What is scope? Click here ➡Manipulating the OAuth2 scope| Apigee | Google Cloud

approach

As a workaround, you can do either of the following two methods.

  1. In settings.py, set ʻACCOUNT_EMAIL_REQUIRED = True`
  2. In settings.py, set the scope of the google provider with SOCIALACCOUNT_PROVIDERS

1. In settings.py, set ʻACCOUNT_EMAIL_REQUIRED = True`

setting.py


...
ACCOUNT_EMAIL_REQUIRED=True
...

With this setting, django-allauth will do a good job so that you can get email address information not only from google but also from other providers. For example, in the case of the google provider, in addition to the `profileʻAPI, the ʻemailʻAPI will be added to the default scope without permission. (For more information, see githu)

2. In settings.py, set the scope of the google provider with SOCIALACCOUNT_PROVIDERS

settings.py


SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
         ],
    },
}

In this setting, unlike method 1, the API to be used directly is specified.

Summary

The above is the cause and remedy for the event that email information cannot be obtained from the provider with django-allauth.

reference

Recommended Posts

I was addicted to not being able to get an email address from google with django-allauth authentication
I was addicted to not being able to use Markdown on pypi's long_description
I was able to implement web app authentication with flask-login
A story I was addicted to trying to get a video url with tweepy
Use Python from Java with Jython. I was also addicted to it.
Send an email to Spushi's address with python
I was addicted to scraping with Selenium (+ Python) in 2020
[IOS] GIF animation with Pythonista3. I was addicted to it.
I tried sending an email from Amazon SES with Python
A story that I was addicted to calling Lambda from AWS Lambda.
The story of not being able to run pygame with pycharm
What I was addicted to with json.dumps in Python base64 encoding
I get an error when trying to install maec 4.0.1.0 with pip
I tried sending an email from the Sakura server with flask-mail
I was addicted to multiprocessing + psycopg2
I was able to print the thermal printer "PAPERANG" from Python (Windows10, Python3.6)
About launching an instance with an encrypted EBS volume (where I was addicted)
I was addicted to running tensorflow on GPU with NVIDIA driver 440 + CUDA 10.2
Send an email to a specific email address with python without SMTP settings
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to creating a Python venv environment with VS Code
I tried to get the authentication code of Qiita API with Python.
[Python] I was addicted to not saving internal variables of lambda expressions
I was addicted to trying Cython with PyCharm, so make a note
I get an error with import pandas.
I tried to get started with Hy
I was addicted to pip install mysqlclient
I tried sending an email with python.
When I get an error with PyInstaller
Get data from an oscilloscope with pyVISA
What I was addicted to Python autorun
[Note] A story about not being able to break through a proxy with pip
Three things I was addicted to when using Python and MySQL with Docker
I was able to mock AWS-Batch with python, moto, so I will leave it
I tried to extract a line art from an image with Deep Learning
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python