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 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. Figure: Contents of the created account after google social login (check on the management screen)
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
As a workaround, you can do either of the following two methods.
SOCIALACCOUNT_PROVIDERS
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)
SOCIALACCOUNT_PROVIDERS
settings.py
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
},
}
In this setting, unlike method 1, the API to be used directly is specified.
The above is the cause and remedy for the event that email information cannot be obtained from the provider with django-allauth.
Recommended Posts