Showing posts with label facebook. Show all posts
Showing posts with label facebook. Show all posts

Sunday, January 30, 2011

Facebook and Pinax Avatars

Pinax is bundled with an app for managing avatars. If your users are uploading their photos, then using the avatar app is pretty straight forward. If you are grabbing a avatar from a users Facebook site, then things are not as easy.

The photo can be accessed using:

file = urllib.urlopen("https://graph.facebook.com/%s/picture?type=normal"%(user['id'],))

The problem comes when you try to save the photo to an avatar using something like this:

avatar=Avatar(user=request.user)
new_file = avatar.avatar.save(path, file.read())
avatar.save()

I was able to get it to work by saving it to a temp file, then wrapping it in a django File:

from tempfile import NamedTemporaryFile
from django.core.files import File
from avatar.models import Avatar,avatar_file_path

file = urllib.urlopen("https://graph.facebook.com/%s/picture?type=normal"%(user['id'],)) fp=NamedTemporaryFile(delete=True)
fp.write(file.read())
avatar=Avatar(user=request.user)
path = avatar_file_path(user=request.user,filename='facebook_%s.jpg'%date.today())
avatar.avatar.save(os.path.join(settings.MEDIA_ROOT,path),File(fp))
avatar.save()
fp.close()

I tried using StringIO, but could not get it to work.

Sunday, January 23, 2011

Django Social-registration and the Facebook API

I am creating a Pinax based website that will require users to have an account and to login. I wanted users with Facebook accounts to be able to use OAuth and Facebook to create their account on my site. I also wanted to use data from Facebook to fill in as much of the user profile as possible. To accomplish this, I installed the app social-registration.

Facebook provides basic info such as first name and last name without extended permissions. Email address requires extended access. Extended access can be requested using the scope parameter during facebook login.

Social-registration has some javascript (templates/socialregistration/facebook_js.html ) that you can put in your templates to allow users to login with facebook. In that js file, the code that adds the scope parameter is commented out:

FB.login(handleResponse/*,{perms:'publish_stream,sms,offline_access,email,read_stream,status_update,etc'}*/);

Uncommenting that code solves part of the problem. However, the perms list is really just a placeholder. status_update and etc are not valid perms, but the rest are. Remove those and it should work.

If you put the social-registration middleware in your settings, then you can access all the users facebook info in your views using:

data_dict=request.facebook.graph.get_object('me')