def weibo_request(self, path, callback, access_token=None, post_args=None, **args): url = "http://" + self._OAUTH_API_DOMAIN + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters(url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) callback = self.async_callback(self._on_weibo_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def twitter_request(self, path, callback, access_token=None, post_args=None, **args): # Add the OAuth resource request signature if we have credentials # NOTE varibles:: # :url used to send request, and bear encoded `args`. # :args keyword-arguments that additionaly added to oauth parameters, # lay on `url`. # :post_args use to judge request method, must be passed as post-data # :all_args as every argument in request take activity # when oauth-parameters is generated, `all_args` contain `args` and `post_args` url = "http://api.twitter.com/1" + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) callback = self.async_callback(self._on_twitter_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def request(self, method, path, data=None, json=False, files=None, cookies=None, **kwgs): kwgs['method'] = method ## `path` should be utf-8 encoded string to complete requote process #if isinstance(path, str): # path = path.encode('utf8') path = requote_uri(path) # `body` must be passed if method is one of those three if method in ['POST', 'PUT', 'PATCH']: headers = kwgs.setdefault('headers', {}) body = '' if files: boundary = '1234567890' headers['Content-Type'] = 'multipart/form-data; boundary=%s' % boundary L = [] if data: for k, v in data.items(): L.append('--' + boundary) L.append('Content-Disposition: form-data; name="%s"' % k) L.append('') L.append(v) for k, f in files.items(): L.append('--' + boundary) L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (k, f[0])) L.append('Content-Type: %s' % mimetypes.guess_type(f[0])[0] or 'application/octet-stream') L.append('') L.append(f[1]) L.append('--%s--' % boundary) L.append('') body = '\r\n'.join(L) else: if data: if json: body = json_encode(data) headers['Content-Type'] = 'application/json' else: headers['Content-Type'] = 'application/x-www-form-urlencoded' body = urlencode(data) kwgs['body'] = body else: if data: path = '%s?%s' % (path, urlencode(data)) if cookies: self._add_cookies(cookies, kwgs) #print 'fetch kwgs', kwgs url = self.get_url(path) logging.debug('testing fetch url: %s', url) self.http_client.fetch(url, self.stop, **kwgs) resp = self.wait() self._parse_cookies(resp) return resp
def tencent_request(self, path, callback, access_token=None, post_args=None, **args): # as Tencent is so f*****g shit # that it use OAuth1.0a but only let the parameter # ``oauth_version`` pass value of 1.0, # there will be very f*****g weird problem occurs # if we don't manually change the _OAUTH_VERSION to be 1.0 # before oauth parameters are generated self._OAUTH_VERSION = '1.0' url = quote("http://" + self._OAUTH_API_DOMAIN + path, ':/') # reset `format` value in args args['format'] = 'json' if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) http = httpclient.AsyncHTTPClient() callback = self.async_callback(self._on_tencent_request, callback) if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def facebook_request(self, path, callback, access_token=None, post_args=None, **args): url = "https://graph.facebook.com" + path all_args = {} if access_token: all_args["access_token"] = access_token all_args.update(args) all_args.update(post_args or {}) if all_args: url += "?" + urlencode(all_args) callback = self.async_callback(self._on_facebook_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def weibo_request(self, path, callback, access_token=None, post_args=None, **args): url = "http://" + self._OAUTH_API_DOMAIN + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) callback = self.async_callback(self._on_weibo_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def tencent_request(self, path, callback, access_token=None, post_args=None, **args): # as Tencent is so f*****g shit # that it use OAuth1.0a but only let the parameter # ``oauth_version`` pass value of 1.0, # there will be very f*****g weird problem occurs # if we don't manually change the _OAUTH_VERSION to be 1.0 # before oauth parameters are generated self._OAUTH_VERSION = '1.0' url = quote("http://" + self._OAUTH_API_DOMAIN + path, ':/') # reset `format` value in args args['format'] = 'json' if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters(url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) http = httpclient.AsyncHTTPClient() callback = self.async_callback(self._on_tencent_request, callback) if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def twitter_request(self, path, callback, access_token=None, post_args=None, **args): # Add the OAuth resource request signature if we have credentials # NOTE varibles:: # :url used to send request, and bear encoded `args`. # :args keyword-arguments that additionaly added to oauth parameters, # lay on `url`. # :post_args use to judge request method, must be passed as post-data # :all_args as every argument in request take activity # when oauth-parameters is generated, `all_args` contain `args` and `post_args` url = "http://api.twitter.com/1" + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters(url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urlencode(args) callback = self.async_callback(self._on_twitter_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def facebook_request(self, method, callback, **args): self.require_setting("facebook_api_key", "Facebook Connect") self.require_setting("facebook_secret", "Facebook Connect") if not method.startswith("facebook."): method = "facebook." + method args["api_key"] = self.settings["facebook_api_key"] args["v"] = "1.0" args["method"] = method args["call_id"] = str(int(time.time() * 1e6)) args["format"] = "json" args["sig"] = self._signature(args) url = "http://api.facebook.com/restserver.php?" + \ urlencode(args) http = httpclient.AsyncHTTPClient() http.fetch(url, callback=self.async_callback( self._parse_response, callback))
def facebook_request(self, method, callback, **args): self.require_setting("facebook_api_key", "Facebook Connect") self.require_setting("facebook_secret", "Facebook Connect") if not method.startswith("facebook."): method = "facebook." + method args["api_key"] = self.settings["facebook_api_key"] args["v"] = "1.0" args["method"] = method args["call_id"] = str(int(time.time() * 1e6)) args["format"] = "json" args["sig"] = self._signature(args) url = "http://api.facebook.com/restserver.php?" + \ urlencode(args) http = httpclient.AsyncHTTPClient() http.fetch(url, callback=self.async_callback(self._parse_response, callback))
def douban_request(self, path, callback, access_token=None, post_args=None, **args): # due to some special string like ``@`` may appear in url, # and they are required to be quoted before generated to be oauth parameters, # (unfortunately tornado don't voluntarily do that) # we forwardly quote the url before it is handled. url = quote("http://" + self._OAUTH_API_DOMAIN + path, ':/') # reset `format` value in args args['alt'] = 'json' if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters(url, access_token, all_args, method=method) args.update(oauth) http = httpclient.AsyncHTTPClient() callback = self.async_callback(self._on_douban_request, callback) if post_args is not None: # douban says that they can't deal request properly # when oauth parameters passing in post data, # instead, passing it in HTTP Headers # NOTE that `url` here is different from `url` in below fetch function: # it is plain, doesn't contain encoded args. http.fetch(url, method="POST", headers=args, body=post_args, callback=callback) else: if args: url += "?" + urlencode(args) http.fetch(url, callback=callback)
def authenticate_redirect(self, callback_uri=None, cancel_uri=None, extended_permissions=None): """Authenticates/installs this app for the current user.""" self.require_setting("facebook_api_key", "Facebook Connect") callback_uri = callback_uri or self.request.uri args = { "api_key": self.settings["facebook_api_key"], "v": "1.0", "fbconnect": "true", "display": "page", "next": urljoin(self.request.full_url(), callback_uri), "return_session": "true", } if cancel_uri: args["cancel_url"] = urljoin(self.request.full_url(), cancel_uri) if extended_permissions: if isinstance(extended_permissions, (str, bytes_type)): extended_permissions = [extended_permissions] args["req_perms"] = ",".join(extended_permissions) self.redirect("http://www.facebook.com/login.php?" + urlencode(args))
def authenticate_redirect(self, callback_uri=None, cancel_uri=None, extended_permissions=None): """Authenticates/installs this app for the current user.""" self.require_setting("facebook_api_key", "Facebook Connect") callback_uri = callback_uri or self.request.uri args = { "api_key": self.settings["facebook_api_key"], "v": "1.0", "fbconnect": "true", "display": "page", "next": urljoin(self.request.full_url(), callback_uri), "return_session": "true", } if cancel_uri: args["cancel_url"] = urljoin( self.request.full_url(), cancel_uri) if extended_permissions: if isinstance(extended_permissions, (str, bytes_type)): extended_permissions = [extended_permissions] args["req_perms"] = ",".join(extended_permissions) self.redirect("http://www.facebook.com/login.php?" + urlencode(args))
def douban_request(self, path, callback, access_token=None, post_args=None, **args): # due to some special string like ``@`` may appear in url, # and they are required to be quoted before generated to be oauth parameters, # (unfortunately tornado don't voluntarily do that) # we forwardly quote the url before it is handled. url = quote("http://" + self._OAUTH_API_DOMAIN + path, ':/') # reset `format` value in args args['alt'] = 'json' if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) http = httpclient.AsyncHTTPClient() callback = self.async_callback(self._on_douban_request, callback) if post_args is not None: # douban says that they can't deal request properly # when oauth parameters passing in post data, # instead, passing it in HTTP Headers # NOTE that `url` here is different from `url` in below fetch function: # it is plain, doesn't contain encoded args. http.fetch(url, method="POST", headers=args, body=post_args, callback=callback) else: if args: url += "?" + urlencode(args) http.fetch(url, callback=callback)
def request(self, method, path, data=None, json=False, files=None, cookies=None, **kwgs): kwgs['method'] = method ## `path` should be utf-8 encoded string to complete requote process #if isinstance(path, str): # path = path.encode('utf8') path = requote_uri(path) # `body` must be passed if method is one of those three if method in ['POST', 'PUT', 'PATCH']: headers = kwgs.setdefault('headers', {}) body = '' if files: boundary = '1234567890' headers[ 'Content-Type'] = 'multipart/form-data; boundary=%s' % boundary L = [] if data: for k, v in data.items(): L.append('--' + boundary) L.append('Content-Disposition: form-data; name="%s"' % k) L.append('') L.append(v) for k, f in files.items(): L.append('--' + boundary) L.append( 'Content-Disposition: form-data; name="%s"; filename="%s"' % (k, f[0])) L.append('Content-Type: %s' % mimetypes.guess_type(f[0])[0] or 'application/octet-stream') L.append('') L.append(f[1]) L.append('--%s--' % boundary) L.append('') body = '\r\n'.join(L) else: if data: if json: body = json_encode(data) headers['Content-Type'] = 'application/json' else: headers[ 'Content-Type'] = 'application/x-www-form-urlencoded' body = urlencode(data) kwgs['body'] = body else: if data: path = '%s?%s' % (path, urlencode(data)) if cookies: self._add_cookies(cookies, kwgs) #print 'fetch kwgs', kwgs url = self.get_url(path) logging.debug('testing fetch url: %s', url) self.http_client.fetch(url, self.stop, **kwgs) resp = self.wait() self._parse_cookies(resp) return resp