I googled and got some helpful link for getting gmail avatar. I used social-auth-app-django library and following the link to setup functionality. Authentication works fine but stuck in getting avatar.I created pipeline.py inside project main configured root and called this inside my view like as from TestDjangoAuth.pipeline import get_avatar. Is this the right way i'm going through to retrieve the avatar? Another query is how to use the pipeline method in views.py like user_details, get_username that we called in SOCIAL_AUTH_PIPELINE
This is the redirect method in views.py which gives some error. i wanna set avatar as session:
from TestDjangoAuth.pipeline import get_avatar
def social_login(request):
if request.method == 'GET':
if request.user.is_authenticated:
request.session['photo'] = get_avatar()
This the pipeline.py that i modified to use in my view
def get_avatar(backend, strategy, details, response, user=None, *args, **kwargs):
url = None
if backend.name == 'google-oauth2':
url = response['image'].get('url')
print(url)
return url
When i return url to use in my view to use profile picture this gives the following error
AttributeError at /auth/complete/google-oauth2/
'str' object has no attribute 'backend'
I finally solved my problem using below code snippet by googling and applying some modification.
def get_avatar(request, backend, strategy, details, response, user=None, *args, **kwargs):
url = None
# if backend.name == 'facebook':
# url = "http://graph.facebook.com/%s/picture?type=large"%response['id']
# if backend.name == 'twitter':
# url = response.get('profile_image_url', '').replace('_normal','')
if backend.name == 'google-oauth2':
try:
url = response["picture"]
except KeyError:
url = response['image'].get('url')
get_file = download(url)
file_name = url.split('/')[-1]
extension = 'jpeg'
f = BytesIO(get_file)
out = BytesIO()
image = Image.open(f)
image.save(out, extension)
def download(url):
try:
r = requests.get(url)
if not r.status_code == 200:
raise Exception('file request failed with status code: ' + str(r.status_code))
return (r.content)
except Exception as ex:
return ('error')