Fetching the Google profile picture while using Django Social Auth
We will be using the social-auth-app-django package to manage OAuth for our website.
You can follow the docs for social auth to implement the basic version of Google OAUTH.
To also fetch the Google Profile Picture you need two more steps,
1. Add a new function in your views.
As you can see, I have a model named UserProfile(one-to-one relation with the User Model) that has a field called picture. I am creating a new UserProfile object with the current user, and the newly fetched picture url and saving it.
2. Add a path to this function in the SOCIAL_AUTH_PIPELINE variable in your settings.py. This will make sure that your function is called each time a user logs in using OAuth.
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
...
'social_core.pipeline.social_auth.associate_by_email',
'posts.views.update_user_social_data'
)
This will run each time the user logs in using Google.
If you want it to only run the first time a user registers on the website, you can add the following condition.
if kwargs[‘is_new’]:
And that’s it.