def get_auth_http_client(self): """Returns the `.AsyncHTTPClient` instance to be used for auth requests. May be overridden by subclasses to use an HTTP client other than the default. """ return httpclient.AsyncHTTPClient()
def youdao_request(self, path, callback, access_token=None, post_args=None, **args): url = "%s/%s" % (self._BASE_URL, path) 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) all_args.update(oauth) args = all_args callback = self.async_callback(self._on_youdao_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: http.fetch(url, method="POST", body=urllib.urlencode(args), callback=callback) else: http.fetch(url_concat(url, args), callback=callback)
def douban_request(self, path, callback, access_token=None, post_args=None, **args): url = "http://api.douban.com" + path if access_token: all_args = {} all_args.update(args) #all_args.update(post_args or {}) #consumer_token = self._oauth_consumer_token() 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) callback = self.async_callback(self._on_douban_request, callback) http = httpclient.AsyncHTTPClient() if post_args is not None: headers = _to_header(args) headers['Content-Type'] = 'application/atom+xml; charset=utf-8' http.fetch(url, method="POST", headers=headers, body=post_args, callback=callback) else: http.fetch(url_concat(url, args), callback=callback)
def get_authenticated_user(self, redirect_uri, client_id, client_secret, code, callback, extra_fields=None): """Handles the login for the Weibo user, returning a user object. Example usage:: class WeiboLoginHandler(LoginHandler, WeiboMixin): @tornado.web.asynchronous def get(self): if self.get_argument("code", False): self.get_authenticated_user( redirect_uri='/auth/weibo/', client_id=self.settings["weibo_client_id"], client_secret=self.settings["weibo_client_secret"], code=self.get_argument("code"), callback=self.async_callback(self._on_login)) return self.authorize_redirect(redirect_uri='/auth/weibo/', client_id=self.settings["weibo_client_id"], extra_params={"response_type": "code"}) def _on_login(self, user): logging.error(user) self.finish() """ http = httpclient.AsyncHTTPClient() args = { "redirect_uri": redirect_uri, "code": code, "client_id": client_id, "client_secret": client_secret, "extra_params": { "grant_type": "authorization_code" }, } post_args = args.copy() post_args.update({"grant_type": "authorization_code"}) fields = set(['uid']) if extra_fields: fields.update(extra_fields) http.fetch(self._oauth_request_token_url(**args), method="POST", body=urllib.urlencode(post_args), callback=self.async_callback(self._on_access_token, redirect_uri, client_id, client_secret, callback, fields))
def recaptcha_validate(self, callback): token = self._recaptcha_token() challenge = self.get_argument('recaptcha_challenge_field', None) response = self.get_argument('recaptcha_response_field', None) callback = self.async_callback(self._on_recaptcha_request, callback) http = httpclient.AsyncHTTPClient() post_args = { 'privatekey': token['secret'], 'remoteip': self.request.remote_ip, 'challenge': challenge, 'response': response } http.fetch(self.RECAPTCHA_VERIFY_URL, method="POST", body=urllib.urlencode(post_args), callback=callback)
def _weibo_upload_request(self, url, callback, access_token, pic, status=None): # build and send the multipart/form-data request if pic is None: raise Exception("pic is required!") form = MultiPartForm() form.add_file("pic", pic["filename"], pic["content"], pic["mime_type"]) form.add_field("status", status) headers = {"Content-Type": form.get_content_type()} args = {"access_token": access_token} url += "?" + urllib.urlencode(args) http = httpclient.AsyncHTTPClient() http.fetch(url, method="POST", body=str(form), callback=self.async_callback(self._on_weibo_request, callback), headers=headers)
def youdao_multipart_post(self, _path, callback, access_token, **post_args): url = "%s/%s" % (self._BASE_URL, _path) oauth = self._oauth_request_parameters(url, access_token, {}, method='POST') args = {} args.update(oauth) callback = self.async_callback(self._on_youdao_request, callback) http = httpclient.AsyncHTTPClient() headers = self._to_header(args) form = MultiPartForm() for k in post_args: form.add_field(k, post_args[k]) headers['Content-Type'] = form.get_content_type() http.fetch(url, method="POST", headers=headers, body=str(form), callback=callback)
def weibo_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(tornado.web.RequestHandler, WeiboMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.weibo_request( "/statuses/update.json", post_args={"status": "I am posting from my Tornado application!"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") To send a request to ``/statuses/upload``, the ``pic`` parameter is required and it should be a dict with ``filename``, ``content`` and ``mime_type`` set. Example usage:: class UploadHandler(tornado.web.RequestHandler, WeiboMixin): @tornado.web.authenticated @tornado.web.asynchronous @tornado.gen.engine def get(self): # ... f = open('foo.png', 'r') # open the image file pic = { 'filename': 'foo.png', # must present, but the value does not matter 'content': f.read(), 'mime_type': 'image/png' } f.close() result = yield tornado.gen.Task(self.weibo_request, '/statuses/upload.json', access_token=self.current_user["access_token"], status='I like this photo!', pic=pic ) # do something with the result ... """ url = "https://api.weibo.com/2" + path if path == "/statuses/upload.json": # this request should be handled differently return self._weibo_upload_request(url, callback, access_token, args.get("pic"), status=args.get("status")) 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 += "?" + urllib.urlencode(all_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=urllib.urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback)
def get_http_client(self): return httpclient.AsyncHTTPClient()