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), {})