Example #1
0
    def create(self, request):
        access_token = request.CLEANED['access_token']
        api = FacebookAPI(access_token)

        try:
            if not api.is_authenticated():
                raise APIException(api_errors.ERROR_FACEBOOK_INVALID_TOKEN)
            access_token, expires = req_exchange_long_lived_access_token(access_token)
            api = FacebookAPI(access_token)
        except HTTPError:
            raise APIException(api_errors.ERROR_FACEBOOK_BAD_REQUEST)

        if not request.user.is_authenticated():
            user, created = api.connect()
            local_fb_profile = user.get_profile().get_fb_profile()
            user = authenticate(facebook_id=local_fb_profile.facebook_id)
            auth_login(request, user)

        user = request.user

        local_fb_profile = user.get_profile().get_fb_profile()
        if not local_fb_profile:
            local_fb_profile = api.attach_local_profile(user)
        local_fb_profile.is_active = True
        local_fb_profile.save()

        gets = request.CLEANED.get('gets', False)

        return wrap_info(user.get_profile().to_json(request, detail=True), {})
Example #2
0
 def _create_test_user(self):
     fb = FacebookAPI(access_token=self._auth_token)
     user_args = {
         'installed': "true",
         'permissions': "offline_access,email",
     }
     user_url = "%s/accounts/test-users" % settings.FACEBOOK_APP_ID
     return fb.request(user_url, user_args, {})            
Example #3
0
 def test_get_or_create_local_profile(self):
     # TODO: verify connection.queries is accurate with MongoDB, looks
     # like only finds are recorded
     old_debug = settings.DEBUG
     settings.DEBUG = True
     connection.queries = []
     
     # no local user or fb profile
     api = FacebookAPI(access_token=test_env.u1['access_token'])
     remote_profile = api.get_profile()
     p1 = api.get_or_create_local_profile()
     self.assertEquals(p1, 
         FacebookProfile.objects.get(facebook_id=test_env.u1['id']))
     self.assertEquals(p1.user,
         User.objects.get(username=remote_profile['email']))        
             
     # local user, no fb profile
     api2 = FacebookAPI(access_token=test_env.u2['access_token'])
     remote_profile2 = api2.get_profile()
     u2 = User.objects.create_user(username=remote_profile2['email'],
         email=remote_profile2['email'], password="******")
     p2 = api2.get_or_create_local_profile()
     self.assertEquals(p2,
         FacebookProfile.objects.get(facebook_id=test_env.u2['id']))
     self.assertEquals(p2.user, u2)
     
     # local user, local fb profile
     p2 = api2.get_or_create_local_profile()
     self.assertEquals(p2,
         FacebookProfile.objects.get(facebook_id=test_env.u2['id']))
     self.assertEquals(FacebookProfile.objects.all().count(), 2)
     self.assertEquals(User.objects.all().count(), 3) # Add one for AnonymousUser
  
     settings.DEBUG = old_debug
Example #4
0
def connect(request):
    """ Facebok-based signup/login """
    
    fb_user = get_user_from_cookie(request.COOKIES, 
        settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET)
    if not fb_user:
        return redirect('registration-signup')
    fb = FacebookAPI(fb_user['access_token'])
    
    # make sure the token is actually valid
    if not fb.is_authenticated():
        return redirect('/')
    
    Registration.objects.create(email=fb.get_profile()['email'])
    messages.info(request, "Thanks for signing up.  We'll let you know when the beta " \
        "is available")
    
    # TODO: Is this really the desired redirect?
    return redirect('/')
Example #5
0
 def test_get_friends(self):
     api = FacebookAPI(access_token=test_env.u1['access_token'])
     u1_friends = api.get_connections(test_env.u1['id'], 'friends')
     self.assertEqual(u1_friends['data'][0]['id'], test_env.u2['id'])
Example #6
0
 def test_is_authenticated(self):
     api = FacebookAPI(access_token=test_env.u1['access_token'])
     self.assertTrue(api.is_authenticated())
     
     api2 = FacebookAPI()
     self.assertFalse(api2.is_authenticated())
Example #7
0
 def test_get_profile(self):
     api = FacebookAPI(access_token=test_env.u1['access_token'])
     self.assertEqual(api.get_profile()['id'], test_env.u1['id'])
Example #8
0
 def _delete_test_user(self, uid):
     fb = FacebookAPI(access_token=self._auth_token)
     fb.delete_object(uid)
Example #9
0
 def _make_friends(self, u1, u2):
     for pair in [(u1, u2), (u2, u1)]:
         fb = FacebookAPI(access_token=pair[0]['access_token'])
         fb.request("%s/friends/%s" % (pair[0]['id'], pair[1]['id']), post_args={})            
     return True
Example #10
0
 def test_get_friend_ids_list(self): 
     api = FacebookAPI(access_token=test_env.u1['access_token'])
     
     self.assertEquals(set([test_env.u2['id'], test_env.u3['id']]),
         set(api.get_friend_ids_list()))
Example #11
0
File: views.py Project: gage/proto
def connect(request):
    """ Facebok-based signup/login.  Will try the following in order:
    
    1.  If this is an existing user, log them in and redirect.
    2.  If we can attach this fb user to an existing user by email, create a local FacebookProfile,
        log them in and redirect.
    3.  If we can create a user with the email address and username given, create the user, create a
        local FacebookProfile, log them in and redirect.
    4.  Ask the user for their username, creates a new user, etc.
    """
    fb_user = get_user_from_cookie(request.COOKIES, 
        settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET)
    if not fb_user:
        return redirect('registration-signup')
    fb = FacebookAPI(fb_user['access_token'])
    
    # make sure the token is actually valid
    if not fb.is_authenticated():
        return redirect('registration-signup')
    
    fb_profile = fb.get_profile()
    fb_local_profile = fb.get_local_profile()
    
    # Case 1, existing user
    if fb_local_profile is not None:
        # we're already attached to a site user so log them in and redirect
        login_fb_user(request, fb_local_profile.facebook_id)
        return redirect(FB_LOGIN_REDIRECT)
    
    # Case 2, try to attach to an existing profile
    fb_email = fb_profile['email']
    try:
        user = User.objects.get(email=fb_email)
        fb_profile = fb.create_local_profile(user)
        login_fb_user(request, fb_profile.facebook_id)
        return redirect(FB_LOGIN_REDIRECT)
    except User.DoesNotExist:
        pass    
    try:
        user = User.objects.get(username=fb_email)
        fb_profile = fb.create_local_profile(user)
        login_fb_user(request, fb_profile.facebook_id)
        return redirect(FB_LOGIN_REDIRECT)
    except User.DoesNotExist:
        pass
    # Case 3, see if username is OK    
    try:
        fb_username = fb_profile['username']
    except KeyError:
        try:
            fb_username = fb_profile['email'].split('@')[0]
        except:
            fb_username = None
    if not fb_username:
        fb_username = None
    
    user_exist = User.objects.filter(username=fb_username).exists()
    serial = 0
    origin_username = fb_username
    while user_exist:
        fb_username = '******'%(origin_username, serial)
        user_exist = User.objects.filter(username=fb_username).exists()
    user = fb.create_user(fb_username, fb_email)
    login_fb_user(request, fb_profile['id'])
    return redirect(FB_LOGIN_REDIRECT)