def post_object(self, path, data=None, file_data=None): if not 'access_token' in data and self.access_token: data['access_token'] = self.access_token url = "https://graph.facebook.com/" + self.api_version + "/%s" % str(path) if not data: data = {} try: if file_data: form = MultiPartForm() for key, val in iteritems(data): form.add_field(key, val) for key, val in file_data.iteritmes(): form.add_file(key, val['name'], val['source'], mimetype=val.get('mimetype')) request = Request(url) body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) request.data =body ret = urlopen(request).read() else: ret = urlopen(url, urlencode(data)).read() return json.loads(ret) except HTTPError as e: self._error_handle(e) return {}
def add_params_to_url(url, params): if not params: return url param_string = urlencode(params) scheme, host, path, cur_params, query, fragment = urlparse.urlparse(url) if query: query = query + '&' + param_string else: query = param_string return urlparse.urlunparse((scheme, host, path, cur_params, query, fragment))
def get_access_token(self, redirect_uri, code): url = "oauth/access_token?%s" % (urlencode({ 'client_id': self.app_id, 'redirect_uri': redirect_uri, 'client_secret': self.app_secret, 'code': code})) parsed = urlparse.parse_qs(urlopen(url).read()) access_token = parsed['access_token'] if type(access_token) is list or type(access_token) is tuple: access_token = access_token[0] self.access_token = access_token return access_token
def get_access_token_from_short_token(self, short): url = "https://graph.facebook.com/oauth/access_token?%s" % \ (urlencode({'client_id': self.app_id, 'grant_type': 'fb_exchange_token', 'client_secret': self.app_secret, 'fb_exchange_token': short})) parsed = urlparse.parse_qs(urlopen(url).read()) access_token = parsed['access_token'] if type(access_token) is list or type(access_token) is tuple: access_token = access_token[0] self.access_token = access_token return access_token
def get_access_token_and_expire_timestamp(self, request, redirect_uri): if not request.session.has_key('facebook_state'): return None, None elif request.session['facebook_state'] != request.GET.get('state'): return None, None url = "https://graph.facebook.com/oauth/access_token?%s" % (urlencode({ 'client_id': self.app_id, 'redirect_uri': redirect_uri, 'client_secret': self.app_secret, 'code': request.GET.get('code')})) parsed = urlparse.parse_qs(urlopen(url).read()) access_token = parsed['access_token'] expires = parsed['expires'] if type(access_token) is list or type(access_token) is tuple: access_token = access_token[0] expires = expires[0] expires = int(expires) + int(time.time()) self.access_token = access_token return access_token, expires
def get_object(self, path, **kwargs): if not 'access_token' in kwargs and self.access_token: kwargs['access_token'] = self.access_token url = "https://graph.facebook.com/" + self.api_version + "/%s?%s" % (str(path), urlencode(kwargs)) try: ret = urlopen(url).read() return json.loads(ret) except HTTPError as e: self._error_handle(e)
def get_page_access_token(self, uid, access_token=None): if access_token is None: access_token = self.access_token url = "%s?fields=access_token&%s" % (uid, urlencode({'access_token': access_token})) return self.get_object(url)['access_token']