Пример #1
0
    def init_facebook(self):
        """Sets up the request specific Facebook and User instance"""
        facebook = Facebook()
        user = None

        # initial facebook request comes in as a POST with a signed_request
        if u'signed_request' in self.request.POST:
            facebook.load_signed_request(self.request.get('signed_request'))
            # we reset the method to GET because a request from facebook with a
            # signed_request uses POST for security reasons, despite it
            # actually being a GET. in webapp causes loss of request.POST data.
            self.request.method = u'GET'
            self.set_cookie('u', facebook.user_cookie,
                            datetime.timedelta(minutes=1440))
        elif 'u' in self.request.cookies:
            facebook.load_signed_request(self.request.cookies.get('u'))

        # try to load or create a user object
        if facebook.user_id:
            user = User.get_by_key_name(facebook.user_id)
            if user:
                # update stored access_token
                if facebook.access_token and \
                        facebook.access_token != user.access_token:
                    user.access_token = facebook.access_token
                    user.put()
                user.refresh_data()
                # restore stored access_token if necessary
                if not facebook.access_token:
                    facebook.access_token = user.access_token

            if not user and facebook.access_token:
                me = facebook.api(u'/me', {u'fields': _USER_FIELDS})
                try:
                    friends = [user[u'id'] for user in me[u'friends'][u'data']]
                    user = User(key_name=facebook.user_id,
                                user_id=facebook.user_id,
                                friends=friends,
                                access_token=facebook.access_token,
                                name=me[u'name'],
                                email=me.get(u'email'),
                                picture=me[u'picture'])
                    user.put()
                except KeyError, ex:
                    pass  # ignore if can't get the minimum fields
Пример #2
0
    def init_facebook(self):
        """Sets up the request specific Facebook and User instance"""
        facebook = Facebook()
        user = None

        # initial facebook request comes in as a POST with a signed_request
        if u'signed_request' in self.request.POST:
            facebook.load_signed_request(self.request.get('signed_request'))
            # we reset the method to GET because a request from facebook with a
            # signed_request uses POST for security reasons, despite it
            # actually being a GET. in webapp causes loss of request.POST data.
            self.request.method = u'GET'
            self.set_cookie(
                'u', facebook.user_cookie, datetime.timedelta(minutes=1440))
        elif 'u' in self.request.cookies:
            facebook.load_signed_request(self.request.cookies.get('u'))

        # try to load or create a user object
        if facebook.user_id:
            user = User.get_by_key_name(facebook.user_id)
            if user:
                # update stored access_token
                if facebook.access_token and \
                        facebook.access_token != user.access_token:
                    user.access_token = facebook.access_token
                    user.put()
                user.refresh_data()
                # restore stored access_token if necessary
                if not facebook.access_token:
                    facebook.access_token = user.access_token

            if not user and facebook.access_token:
                me = facebook.api(u'/me', {u'fields': _USER_FIELDS})
                try:
                    friends = [user[u'id'] for user in me[u'friends'][u'data']]
                    user = User(key_name=facebook.user_id,
                        user_id=facebook.user_id, friends=friends,
                        access_token=facebook.access_token, name=me[u'name'],
                        email=me.get(u'email'), picture=me[u'picture'])
                    user.put()
                except KeyError, ex:
                    pass # ignore if can't get the minimum fields
Пример #3
0
    def init_facebook(self):
        """Sets up the request specific Facebook and User instance"""
        facebook = Facebook()
        user = None

        # initial facebook request comes in as a POST with a signed_request
        if u'signed_request' in self.request.POST:
            facebook.load_signed_request(self.request.get('signed_request'))
            # we reset the method to GET because a request from facebook with a
            # signed_request uses POST for security reasons, despite it
            # actually being a GET. in webapp causes loss of request.POST data.
            if facebook.access_token and facebook.user_id:
                if User.get_by_key_name(facebook.user_id):
                    taskqueue.add(url="/update_friend_info",
                                  params={
                                      "user": facebook.user_id,
                                      "access_token": facebook.access_token
                                  },
                                  method='GET')
            self.request.method = u'GET'
            self.set_cookie('u', facebook.user_cookie,
                            datetime.timedelta(minutes=1440))

        elif 'u' in self.request.cookies:
            facebook.load_signed_request(self.request.cookies.get('u'))

        # try to load or create a user object
        if facebook.user_id:
            user = User.get_by_key_name(facebook.user_id)
            if user:
                # update stored access_token
                if facebook.access_token and \
                        facebook.access_token != user.access_token:
                    user.access_token = facebook.access_token
                    user.put()
                # restore stored access_token if necessary
                if not facebook.access_token:
                    facebook.access_token = user.access_token

            if not user and facebook.access_token:
                me = facebook.api(u'/me', {u'fields': u'name,picture,friends'})
                try:

                    friends = [user[u'id'] for user in me[u'friends'][u'data']]
                    friend_names = [
                        user[u'name'] for user in me[u'friends'][u'data']
                    ]
                    friendlist = sorted(zip(friends, friend_names),
                                        key=lambda k: k[1])
                    if not memcache.set("friendlist_" + facebook.user_id,
                                        friendlist,
                                        time=7200):
                        logging.error(
                            "Memcache add failed for key: friendlist_" +
                            facebook.user_id)
                    user = User(key_name=facebook.user_id,
                                user_id=facebook.user_id,
                                friends=friends,
                                friend_names=friend_names,
                                access_token=facebook.access_token,
                                name=me[u'name'],
                                picture=me[u'picture'])
                    user.put()
                    taskqueue.add(url="/update_friend_info",
                                  params={
                                      "user": facebook.user_id,
                                      "access_token": facebook.access_token
                                  },
                                  method='GET')
                except:
                    # ignore if can't get the minimum fields
                    logging.error(
                        "Can't get minimum amount of fields when initializing facebook"
                    )
                    raise

        self.facebook = facebook
        self.user = user
Пример #4
0
    def init_facebook(self):
        """Sets up the request specific Facebook and User instance"""
        facebook = Facebook()
        user = None

        # initial facebook request comes in as a POST with a signed_request
        if u'signed_request' in self.request.POST:
            facebook.load_signed_request(self.request.get('signed_request'))
            # we reset the method to GET because a request from facebook with a
            # signed_request uses POST for security reasons, despite it
            # actually being a GET. in webapp causes loss of request.POST data.
            if facebook.access_token and facebook.user_id:
                if User.get_by_key_name(facebook.user_id):
                    taskqueue.add(url="/update_friend_info",
                                  params={"user": facebook.user_id,
                                          "access_token": facebook.access_token}, method='GET')
            self.request.method = u'GET'
            self.set_cookie(
                'u', facebook.user_cookie, datetime.timedelta(minutes=1440))

        elif 'u' in self.request.cookies:
            facebook.load_signed_request(self.request.cookies.get('u'))

        # try to load or create a user object
        if facebook.user_id:
            user = User.get_by_key_name(facebook.user_id)
            if user:
                # update stored access_token
                if facebook.access_token and \
                        facebook.access_token != user.access_token:
                    user.access_token = facebook.access_token
                    user.put()
                # restore stored access_token if necessary
                if not facebook.access_token:
                    facebook.access_token = user.access_token

            if not user and facebook.access_token:
                me = facebook.api(u'/me', {u'fields': u'name,picture,friends'})
                try:

                    friends = [user[u'id'] for user in me[u'friends'][u'data']]
                    friend_names = [user[u'name'] for user in
                                    me[u'friends'][u'data']]
                    friendlist = sorted(zip(friends,friend_names), 
                                        key = lambda k: k[1])
                    if not memcache.set("friendlist_" + facebook.user_id,
                                        friendlist, time=7200):
                        logging.error("Memcache add failed for key: friendlist_"
                                      + facebook.user_id)
                    user = User(key_name=facebook.user_id,
                                user_id=facebook.user_id, friends=friends,
                                friend_names=friend_names,
                                access_token=facebook.access_token,
                                name=me[u'name'],
                                picture=me[u'picture'])
                    user.put()
                    taskqueue.add(url="/update_friend_info",
                                  params={"user": facebook.user_id, "access_token": facebook.access_token}, method='GET')
                except:
                    # ignore if can't get the minimum fields
                    logging.error("Can't get minimum amount of fields when initializing facebook")
                    raise

        self.facebook = facebook
        self.user = user
Пример #5
0
    def init_facebook(self):
        logging.debug('in init_facebook')
        facebook = Facebook()

        # set key for memcache
        self.key = self.request.cookies.get('fbs_' + FB_APP_ID, '')
        #self.key = self.request.cookies.get('__utma')
        logging.debug(self.key)
        logging.debug(self.request.cookies)
        logging.debug(self.request.cookies.get('fbs_'))
        logging.debug('memcache check')
        
        # try to find the user in memcache
        if self.key:
            user = memcache.get(self.key)
            self.user = user
            logging.debug(self.user)
            logging.debug('get memcache')
            
            # if the user is found in memacache return
            if (self.user and u'signed_request' in self.request.POST):
                # check to see if the user is still in the datastore
                self.request.method = u'GET'
                logging.debug('found in memcache')
                return self.user
            elif self.user: 
                return self.user
            # the initial facebook request is POST and not GET
        if u'signed_request' in self.request.POST:
            """call load_signed_request to check on fb signature"""
            facebook.load_signed_request(self.request.get('signed_request'))
            logging.debug(facebook.user_id)
            """we also want to change the POST request from fb to GET"""
            self.request.method = u'GET'
    
            # load a user object
            if facebook.user_id:
                me = facebook.api(u'/me', {u'fields': _USER_FIELDS_API})
                user = Users.get_by_key_name(facebook.user_id)
                if user:
                    me = facebook.api(u'/me',{u'fields': _USER_FIELDS_API})
                    self.me = me
                    # update access token
                    if facebook.access_token != user.access_token:
                        user.access_token = facebook.access_token
                        user.put()
                        memcache.add(self.key, user)
    
                    # set access_token if doesn't exist
                    if not facebook.access_token:
                        facebook.access_token = user.access_token
                        memcache.add(self.key, user)
                        
                    memcache.add(self.key, user)
    
                # add new user to datastore
                if not user and facebook.access_token:
                    me = facebook.api(u'/me',{u'fields': _USER_FIELDS_API})
                    self.me = me
                    try:
                        friendslist = [user[u'id'] for user in me[u'friends'][u'data']]  
                        user = Users(key_name = facebook.user_id, 
                                     user_id = facebook.user_id,
                                     access_token = facebook.access_token, 
                                     name = me[u'name'], 
                                     email = me[u'email'], 
                                     picture = me[u'picture'],
                                     first_name = me[u'first_name'], 
                                     last_name = me[u'last_name'],
                                     friends = friendslist) #, checkin = foursquare, status = statuslist)
                        user.put()
                        memcache.add(self.key, user)
                        logging.debug('after put and memcache add')
                    except: 
                        pass
    
                self.user = user 
                return self.user