Source

42qu / .app / oauth / view / weibo.py

Full commit
#!/usr/bin/env python
# coding: utf-8
import _env
import logging
import urllib
import tornado.web
from _route import route
from model.base.gid import CID
from view.oauth._base import LoginView, JsonLoginView
from tornado import escape
from tornado.httputil import url_concat
from tornado.auth import httpclient, OAuth2Mixin
from model.oauth.weibo import WeiboMixin
from model.oauth.oauth import oauth_new
from model.oauth.weibo_po import oauth_weibo_save, oauth_weibo_bind_po
from misc.config.oauth import WEIBO_API_KEY, WEIBO_API_SECRET
from misc.config import HOST

@route('/oauth/save/weibo')
class _(JsonLoginView):
    def post(self):
        txt = self.get_argument('txt', '').strip()
        txt = txt.decode('utf-8', 'ignore')[:140]
        if txt:
            oauth_weibo_save(self.current_user_id, txt)
        self.finish('{}')


@route('/oauth/bind/weibo')
class WeiboLoginHandler(WeiboMixin, LoginView):
    @tornado.web.asynchronous
    def get(self):
        redirect_url = 'http://rpc.%s%s' % (HOST, self.request.path)
        if self.get_argument('code', False):
            self.get_authenticated_user(
                redirect_uri=redirect_url,
                client_id=WEIBO_API_KEY,
                client_secret=WEIBO_API_SECRET,
                code=self.get_argument('code'),
                callback=self.async_callback(self._on_login)
            )
        else:
            self.authorize_redirect(
                redirect_uri=redirect_url,
                client_id=WEIBO_API_KEY,
                extra_params={'response_type': 'code'})

    def _on_login(self, user):
        #user_id, cid, key, secret
        #存id, 加积分
        user_id = self.current_user_id
        oauth_new(user_id, CID.USER_NAMECARD_WEIBO, user['code'], user['access_token'])
        weibo_uid = user['uid']
        oauth_weibo_bind_po(user_id, weibo_uid)
        self.redirect("//%s/oauth/binded/weibo"%HOST)