Example #1
0
 def get(self):
     params = {
         'fields': 'id,name,username,picture,location,website,friends',
         'access_token': self.user.fb_access_token
     }
     userInfo = call_api.facebook_api('GET', '/me', params)
     m_userInfo = {
         'facebook_id':
         userInfo['id'],
         'realname':
         userInfo.get('name') or '',
         'username':
         userInfo.get('username') or '',
         'picture':
         userInfo['picture']['data']['url'],
         'location':
         userInfo['location']['name']
         if userInfo.has_key('location') else '',
         'website':
         userInfo.get('website') or '',
         'friends':
         [friend['id'] for friend in userInfo['friends']['data']]
         if userInfo.has_key('friends') else []
     }
     data_models.update_facebook_info(self.user, m_userInfo)
     data_models.update_facebook_friends(self.user, m_userInfo)
     self.redirect('/profile')
Example #2
0
 def get(self):
     params = {'fields':'id,name,username,picture,location,website,friends', 'access_token':self.user.fb_access_token}
     userInfo = call_api.facebook_api('GET', '/me', params)
     m_userInfo = {'facebook_id':userInfo['id'],
                   'realname':userInfo.get('name') or '',
                   'username':userInfo.get('username') or '',
                   'picture':userInfo['picture']['data']['url'],
                   'location':userInfo['location']['name'] if userInfo.has_key('location') else '',
                   'website':userInfo.get('website') or '',
                   'friends':[friend['id'] for friend in userInfo['friends']['data']] if userInfo.has_key('friends') else []}
     data_models.update_facebook_info(self.user, m_userInfo)
     data_models.update_facebook_friends(self.user, m_userInfo)
     self.redirect('/profile')
Example #3
0
    def getFBFriend(self, reload):
        client = memcache.Client()
        #        friend_ids = client.get(self.user.user_id+'fb_fids')
        m_friends = FBFriends.gql('WHERE facebook_id = :1',
                                  self.user.facebook_id).get()
        friend_ids = m_friends.friends
        cursor = client.get(self.user.user_id + 'fb_cursor')
        logging.info('reload:  ' + str(reload))
        logging.info('cursor:  ' + str(cursor))
        #        if not friend_ids or reload:
        if reload:
            #            params = {'fields':'friends', 'access_token': self.user.fb_access_token}
            #            response = call_api.facebook_api('GET', '/me', params)
            #            friend_ids = [friend['id'] for friend in response['friends']['data']]
            cursor = 0
#            client.add(self.user.user_id+'fb_fids', friend_ids)
#            client.add(self.user.user_id+'fb_cursor', cursor)

        if cursor >= len(friend_ids):
            return ({}, 0, m_friends.num_on_cw)

        if cursor + FRIENDS_EACH_BULK > len(friend_ids):
            next_part = friend_ids[cursor:]
        else:
            next_part = friend_ids[cursor:(cursor + FRIENDS_EACH_BULK)]
        cursor += FRIENDS_EACH_BULK

        params = {
            'ids': ','.join([str(id) for id in next_part]),
            'fields': 'id,name,picture'
        }
        response = call_api.facebook_api('GET', '/', params)
        FB_friends = []
        current_cws = 0
        for id, friendInfo in response.items():
            fb_friend = {
                'id': friendInfo['id'],
                'name': friendInfo['name'],
                'picture': friendInfo['picture']['data']['url']
            }
            if id in friend_ids[:m_friends.num_on_cw]:
                tmpuser = User.gql('WHERE facebook_id = :1', id).get()
                fb_friend['uid'] = tmpuser.user_id
                FB_friends.insert(0, fb_friend)
                current_cws += 1
            else:
                FB_friends.append(fb_friend)
        client.set(self.user.user_id + 'fb_cursor', cursor)
        return (FB_friends, current_cws, m_friends.num_on_cw)
    def getFBFriend(self, reload):
        client = memcache.Client()
#        friend_ids = client.get(self.user.user_id+'fb_fids')
        m_friends = FBFriends.gql('WHERE facebook_id = :1', self.user.facebook_id).get()
        friend_ids = m_friends.friends
        cursor = client.get(self.user.user_id+'fb_cursor')
        logging.info('reload:  '+str(reload))
        logging.info('cursor:  '+str(cursor))
#        if not friend_ids or reload:
        if reload:
#            params = {'fields':'friends', 'access_token': self.user.fb_access_token}
#            response = call_api.facebook_api('GET', '/me', params)
#            friend_ids = [friend['id'] for friend in response['friends']['data']]
            cursor = 0
#            client.add(self.user.user_id+'fb_fids', friend_ids)
#            client.add(self.user.user_id+'fb_cursor', cursor)
        
        if cursor >= len(friend_ids):
            return ({}, 0, m_friends.num_on_cw)
            
        if cursor + FRIENDS_EACH_BULK > len(friend_ids):
            next_part = friend_ids[cursor:]
        else:
            next_part = friend_ids[cursor:(cursor + FRIENDS_EACH_BULK)]
        cursor += FRIENDS_EACH_BULK
        
        params = {'ids':','.join([str(id) for id in next_part]), 
                  'fields':'id,name,picture'}
        response = call_api.facebook_api('GET', '/', params)
        FB_friends = []
        current_cws = 0
        for id, friendInfo in response.items():
            fb_friend = {'id':friendInfo['id'], 
                         'name':friendInfo['name'], 
                         'picture':friendInfo['picture']['data']['url']}
            if id in friend_ids[:m_friends.num_on_cw]:
                tmpuser = User.gql('WHERE facebook_id = :1', id).get()
                fb_friend['uid'] = tmpuser.user_id
                FB_friends.insert(0, fb_friend)
                current_cws += 1
            else:
                FB_friends.append(fb_friend)
        client.set(self.user.user_id+'fb_cursor', cursor)
        return (FB_friends, current_cws, m_friends.num_on_cw)
Example #5
0
    def facebook_auth(self, callback):
        code = self.request.get('code')

        # check if the users has already granted access
        if not code:

            # redirecting the user
            self.redirect('https://www.facebook.com/dialog/oauth?' +
                          'client_id=' + conf.FACEBOOK_APP_ID +
                          '&redirect_uri=' + conf.WEB_SERVER + callback +
                          '&scope=user_location,user_website' +
                          '&state=zhicong')

        state = self.request.get('state')

        # param used for cross-site request forgery protection
        if state == 'zhicong':
            tokenUrl = ('https://graph.facebook.com/oauth/access_token?' +
                        'client_id=' + conf.FACEBOOK_APP_ID +
                        '&redirect_uri=' + conf.WEB_SERVER + callback +
                        '&client_secret=' + conf.FACEBOOK_APP_SECRET +
                        '&code=' + code)

            response = urlfetch.fetch(tokenUrl).content
            tokenInfo = self.get_key_value_from_string(response)
            access_token = tokenInfo['access_token']

            #TODO: Check this out.. it is never used
            expires = datetime.datetime.now() + datetime.timedelta(
                seconds=int(tokenInfo['expires']))

            #get info about the user
            params = {
                'fields':
                'id, first_name, last_name, name,username,picture,location,website,friends',
                'access_token': access_token
            }
            userInfo = call_api.facebook_api('GET', '/me', params)
            m_userInfo = {
                'facebook_id':
                userInfo['id'],
                'firstname':
                userInfo.get('first_name').capitalize(),
                'lastname':
                userInfo.get('last_name').capitalize(),
                'realname':
                userInfo.get('name') or '',
                'username':
                userInfo.get('username') or '',
                'picture':
                userInfo['picture']['data']['url'],
                'fb_access_token':
                access_token,
                'location':
                userInfo['location']['name']
                if userInfo.has_key('location') else '',
                'website':
                userInfo.get('website') or '',
                'friends':
                [friend['id'] for friend in userInfo['friends']['data']]
                if userInfo.has_key('friends') else []
            }
            return m_userInfo
        return None
Example #6
0
    def facebook_auth(self, callback):
        code = self.request.get("code")

        # check if the users has already granted access
        if not code:

            # redirecting the user
            self.redirect(
                "https://www.facebook.com/dialog/oauth?"
                + "client_id="
                + conf.FACEBOOK_APP_ID
                + "&redirect_uri="
                + conf.WEB_SERVER
                + callback
                + "&scope=user_location,user_website"
                + "&state=zhicong"
            )

        state = self.request.get("state")

        # param used for cross-site request forgery protection
        if state == "zhicong":
            tokenUrl = (
                "https://graph.facebook.com/oauth/access_token?"
                + "client_id="
                + conf.FACEBOOK_APP_ID
                + "&redirect_uri="
                + conf.WEB_SERVER
                + callback
                + "&client_secret="
                + conf.FACEBOOK_APP_SECRET
                + "&code="
                + code
            )

            response = urlfetch.fetch(tokenUrl).content
            tokenInfo = self.get_key_value_from_string(response)
            access_token = tokenInfo["access_token"]

            # TODO: Check this out.. it is never used
            expires = datetime.datetime.now() + datetime.timedelta(seconds=int(tokenInfo["expires"]))

            # get info about the user
            params = {
                "fields": "id, first_name, last_name, name,username,picture,location,website,friends",
                "access_token": access_token,
            }
            userInfo = call_api.facebook_api("GET", "/me", params)
            m_userInfo = {
                "facebook_id": userInfo["id"],
                "firstname": userInfo.get("first_name").capitalize(),
                "lastname": userInfo.get("last_name").capitalize(),
                "realname": userInfo.get("name") or "",
                "username": userInfo.get("username") or "",
                "picture": userInfo["picture"]["data"]["url"],
                "fb_access_token": access_token,
                "location": userInfo["location"]["name"] if userInfo.has_key("location") else "",
                "website": userInfo.get("website") or "",
                "friends": [friend["id"] for friend in userInfo["friends"]["data"]]
                if userInfo.has_key("friends")
                else [],
            }
            return m_userInfo
        return None