Я работаю над одним из проектов Django, где я использовал social_ django фреймворк, предоставленный командой Django для единого входа в Google. Теперь мое требование - получить изображение профиля, связанное с этой учетной записью Gmail
Ссылка на документацию для единого входа https://python-social-auth.readthedocs.io/en/latest/configuration/django.html
setting.py
INSTALLED_APPS = [
....
'social_django'
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOpenId',
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.debug.debug',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'social.pipeline.debug.debug',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
views.py
@login_required
@transaction.atomic
def update_profile(request):
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
return HttpResponseRedirect('/')
else:
messages.error(request, ('Please correct the error below.'))
else:
user_form = UserForm(instance=request.user)
profile_form = ProfileForm(instance=request.user.profile)
return render(request, 'testapp/profile.html', {
'user_form': user_form,
'profile_form': profile_form
})
models.py
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
form.py
from django import forms
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email',)
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('bio','location','birth_date')
output Когда я входил в свою учетную запись Gmail, то каким-то образом я получал эти данные
{'access_token': ---,
'email': 'xyz@gmail.com',
'email_verified': True,
'expires_in': 3599,
'family_name': 'xyz',
'given_name': 'xyz',
'hd': 'gmail.com',
'id_token': --- ,
'locale': 'en',
'name': 'xyz xyz',
'picture': 'https://lh3.googleusercontent.com/a-qcnZYR0SC4bwr',
'scope': ---,
'sub': ---,
'token_type': ---}
================================================================================
{'email': 'xyz@gmail.com',
'first_name': 'xyz',
'fullname': 'xyz xyz',
'last_name': 'xyz',
'username': 'xyz'}
================================================================================
()
================================================================================
{'backend': <social_core.backends.google.GoogleOAuth2>,
'is_new': False,
'new_association': False,
'pipeline_index': 7,
'request': ---,
'social': <UserSocialAuth: xyz>,
'storage': <class 'social_django.models.DjangoStorage'>,
'strategy': <social_django.strategy.DjangoStrategy>,
'uid': 'xyz@gmail.com',
'user': <User: xyz>,
'username': 'xyz'}
Вопрос 1) Я могу получить всю информацию, связанную с пользователем, как эта информация выходит, что я не понимаю? 2) Как сохранить эту информацию в базе данных (имя пользователя, адрес электронной почты и ссылка на изображение)