我from social_auth.signals import socialauth_registered
在我的项目中使用新注册用户。我注意到当我尝试在 facebook 上注册我的项目时。我的项目没有获得我的 facebook 的个人资料图片,而是获得了我的gravatar.com
帐户的个人资料图片。
这是我的代码:
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from userena.models import *
from social_auth.signals import pre_update
from social_auth.backends.facebook import FacebookBackend
from social_auth.backends import google
from social_auth.signals import socialauth_registered
import datetime
def new_users_handler(sender, user, response, details, **kwargs):
user.is_new = True
print "hello"
if user.is_new:
print "world"
if "id" in response:
print "police"
from urllib2 import urlopen, HTTPError
from django.template.defaultfilters import slugify
from django.core.files.base import ContentFile
try:
url = None
if sender == FacebookBackend:
url = "http://graph.facebook.com/%s/picture?type=large" \
% response["id"]
elif sender == google.GoogleOAuth2Backend and "picture" in response:
url = response["picture"]
print url
if url:
avatar = urlopen(url)
#profile = UserProfile(user=user)
print "again"
print user
fileName = "media/mugshots/"+ str(user) + ".jpg"
print "okss"
print fileName
try:
profile = Profile.objects.get(user=user)
except:
profile = Profile.objects.create(user=user)
localFile = open(fileName, 'w')
localFile.write(avatar.read())
localFile.close()
profile.mugshot = fileName
print "save=ing profile"
#profile.mugshot.save(slugify(user.username + " social") + '.jpg',
# ContentFile(avatar.read()))
profile.save()
except HTTPError:
pass
return False
socialauth_registered.connect(new_users_handler, sender=None)
但我的代码无法将 facebook 个人资料图片保存到“media/mudshots/dir”。
我的问题是,如何获取 facebook 帐户的个人资料图片并将其保存media/mudshots/
在我的 django 项目的目录中?
任何人都可以帮助我处理我的情况吗?
提前致谢 ..