簡體   English   中英

django-social-auth:如何獲取facebook的個人資料圖片並將其保存在media / mudshots /文件夾中

[英]django-social-auth : how to get the profile pic of facebook and save it in media/mudshots/ folder

我正在使用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帳戶的個人資料圖片並將其保存在django項目中的media/mudshots/ dir中?

誰能幫助我解決我的案件?

提前致謝 ..

我明白了...我用這個..

import datetime
import urllib
import string
import random
import os
avatar = urlopen(url)

try:
    profile = Profile.objects.get(user=user)
except:
    profile = Profile.objects.create(user=user)

print profile
print "sdfffffffffffffffffffff"
filename_charset = string.ascii_letters + string.digits
filename_length = 10
file_save_dir = 'media/mugshots/'

filename = ''.join(random.choice(filename_charset)
                   for s in range(filename_length))

urllib.urlretrieve (url, os.path.join(file_save_dir, filename + '.png'))

profile.mugshot = 'mugshots/'+filename + '.png'


profile.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM