Exemple #1
0
    def authenticate(self, fb_access_token):
        """ authenticates the token by requesting user information from facebook """
        try:
            api = facebook.GraphAPI(fb_access_token)
            userinfo = api.get_object("me")
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise
        # variables not initialise here
        try:
            user_profile = FacebookUserProfile.objects.get(
                fb_uid=userinfo['id'])
            if user_profile.user.is_active:
                return user_profile.user
            else:
                return
        except FacebookUserProfile.DoesNotExist:
            #Create new user
            try:
                username = userinfo['username']
            except KeyError:
                # if username not set on facebook
                username = userinfo['first_name']

            user = new_user(username=username, provider='Facebook')

            user.first_name, user.last_name = userinfo['first_name'], userinfo[
                'last_name']
            #img_url = 'http://graph.facebook.com/me/picture?type=large'+'&fb_access_token='+ access_token
            user.save()
            userprofile = FacebookUserProfile(user=user,
                                              fb_uid=userinfo['id'],
                                              fb_username=username,
                                              location=userinfo['location'])
            userprofile.access_token = fb_access_token
            """if img_url:
                img = ContentFile(urlopen(img_url).read())
                name = img_url.split('/')[-1]
                user.picture.save(name, img, False)"""

        userprofile.save()
        AuthMeta(user=user, provider='Facebook').save()
        return user
    def authenticate(self, fb_access_token):
        """ authenticates the token by requesting user information from facebook """
        try:
            api = facebook.GraphAPI(fb_access_token)
            userinfo = api.get_object("me")
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise
        # variables not initialise here
        try:
            user_profile = FacebookUserProfile.objects.get(fb_uid = userinfo['id'])
            if user_profile.user.is_active:
                return user_profile.user
            else:
                return
        except FacebookUserProfile.DoesNotExist:
            #Create new user
            try:
                username = userinfo['username']
            except KeyError:
                # if username not set on facebook
                username = userinfo['first_name']
                       
            user = new_user(username = username, provider = 'Facebook')
                       
            user.first_name, user.last_name = userinfo['first_name'], userinfo['last_name']
            #img_url = 'http://graph.facebook.com/me/picture?type=large'+'&fb_access_token='+ access_token
            user.save()
            userprofile = FacebookUserProfile(user = user, fb_uid = userinfo['id'], fb_username = username, location = userinfo['location'])
            userprofile.access_token = fb_access_token

            """if img_url:
                img = ContentFile(urlopen(img_url).read())
                name = img_url.split('/')[-1]
                user.picture.save(name, img, False)"""
       
        userprofile.save()
        AuthMeta(user=user, provider='Facebook').save()
        return user