Beispiel #1
0
  def generate_auth(self):
    components = [self.formatted_date, self.request_type.upper(), self.host.lower(), self.path]

    args = []
    for key, val in sorted(self.params, key=lambda (k,v): k):
      args.append('{k}={v}'.format(k=urllib.quote(key, '~'), v=urllib.quote(val, '~')))
    components.append('&'.join(args))

    signature_string = '\n'.join(components)
    signature = hmac.new(Secrets.get('secret_key'), signature_string, hashlib.sha1).hexdigest()

    auth_string = '{key}:{signature}'.format(key=Secrets.get('integration_key'), signature=signature)
    return base64.b64encode(auth_string)
Beispiel #2
0
class Graph(object):
    app_id = Secrets.get('app_id')
    version = 2.4
    access_token_store = RedisStore('facelock:facebook:graph:access_tokens')

    def __init__(self, access_token=None):
        self.access_token = access_token or Secrets.get('access_token')
        self._graph = facebook.GraphAPI(self.access_token, self.version)

    @property
    def auth_code(self):
        return AuthCode(self, self.new_auth_code())

    @property
    def fb_id(self):
        return self._graph.get_object('me')['id']

    @classmethod
    def for_user(cls, user_id=None):
        if user_id and cls.access_token_store.exists(user_id):
            access_token = cls.access_token_store.get(user_id)
        else:
            access_token = cls().auth_code.poll()
            if user_id:
                cls.access_token_store.setex(user_id, access_token,
                                             5184000)  # 60 Days
        return Graph(access_token)

    def new_auth_code(self):
        args = {
            'client_id': self.app_id,
            'type': 'device_code',
            'scope': 'user_photos'
        }
        return self._graph.request('oauth/device', post_args=args)

    def poll_auth_code(self, code):
        args = {'client_id': self.app_id, 'type': 'device_token', 'code': code}
        return self._graph.request('oauth/device', post_args=args)

    @photo_collection
    def photos(self):
        FIELDS = 'id,images,tags,width,height'

        id = self.fb_id
        photos = self._graph.get_connections('me', 'photos', fields=FIELDS)
        after = True
        while after:
            for photo in photos['data']:
                yield Photo(photo, id)
            try:
                after = photos['paging']['cursors']['after']
            except KeyError:
                raise StopIteration
            photos = self._graph.get_connections('me',
                                                 'photos',
                                                 fields=FIELDS,
                                                 after=after)
        raise StopIteration
Beispiel #3
0
    def generate_auth(self):
        components = [
            self.formatted_date,
            self.request_type.upper(),
            self.host.lower(), self.path
        ]

        args = []
        for key, val in sorted(self.params, key=lambda (k, v): k):
            args.append('{k}={v}'.format(k=urllib.quote(key, '~'),
                                         v=urllib.quote(val, '~')))
        components.append('&'.join(args))

        signature_string = '\n'.join(components)
        signature = hmac.new(Secrets.get('secret_key'), signature_string,
                             hashlib.sha1).hexdigest()

        auth_string = '{key}:{signature}'.format(
            key=Secrets.get('integration_key'), signature=signature)
        return base64.b64encode(auth_string)
Beispiel #4
0
 def host(self):
     return Secrets.get('api_hostname')
Beispiel #5
0
 def host(self):
   return Secrets.get('api_hostname')
Beispiel #6
0
 def __init__(self, access_token=None):
   self.access_token = access_token or Secrets.get('access_token')
   self._graph = facebook.GraphAPI(self.access_token, self.version)
Beispiel #7
0
 def __init__(self, access_token=None):
     self.access_token = access_token or Secrets.get('access_token')
     self._graph = facebook.GraphAPI(self.access_token, self.version)