Beispiel #1
0
def _get_access_token(appid, refresh=False):
    if not refresh:
        token = wechat_storage.get_access_token(
            appid)  # TODO: move this to redis
        if token:
            raise tornado.gen.Return({
                'err_code': 0,
                'data': {
                    'access_token': token
                }
            })
    try:
        app_info = wechat_storage.get_app_info(appid=appid)
        resp = yield httputils.get_dict(url=url.wechat_basic_access_token,
                                        data={
                                            'grant_type': 'client_credential',
                                            'appid': appid,
                                            'secret': app_info['secret']
                                        })
    except tornado.httpclient.HTTPError:
        raise tornado.gen.Return({'err_code': 1001})
    result = _parse_wechat_resp(resp)
    if result['err_code'] != 0:
        raise tornado.gen.Return(result)
    else:
        result_data = result.get('data')
        wechat_storage.add_access_token(app_info['id'], appid,
                                        result_data.get('access_token'),
                                        result_data.get('expires_in'))
        raise tornado.gen.Return(result)
Beispiel #2
0
 def login(self, appid):
     appinfo = wechat_storage.get_app_info(appid)
     username = appinfo['mp_username']
     pwd = appinfo['mp_pwd']
     try:
         resp = yield self._post_form(appid, url.mp_login, {
             'username': username,
             'pwd': pwd,
             'f': 'json'
         })
     except tornado.httpclient.HTTPError:
         logging.warning('login failed: %s', appid)
         raise tornado.gen.Return({'err_code': 7000})
     result = _parse_mp_resp(resp)
     if result['err_code'] != 0:
         logging.warning('login failed: %s %s', appid, result)
         raise tornado.gen.Return(result)
     result_data = result.get('data')
     self.tokens[appid] = {
         'last_login':
         time.time(),
         'token':
         dict(urllib.parse.parse_qsl(result_data['redirect_url']))['token']
     }
     logging.info('login success: %s', appid)
     raise tornado.gen.Return({
         'err_code': 0,
         'data': {
             'token': self.tokens[appid]['token']
         }
     })
Beispiel #3
0
 def _init_cookies(self, appid):
     appinfo = wechat_storage.get_app_info(appid)
     self.cookies[appid] = {
         'data_bizuin': appinfo['data_bizuin'],
         'slave_user': appinfo['openid'],
         'bizuin': appinfo['fakeid']
     }
Beispiel #4
0
def _get_access_token(appid, refresh=False):
    if not refresh:
        token = wechat_storage.get_access_token(appid)  # TODO: move this to redis
        if token:
            raise tornado.gen.Return({
                'err_code': 0,
                'data': {'access_token': token}
            })
    try:
        app_info = wechat_storage.get_app_info(appid=appid)
        resp = yield httputils.get_dict(
            url=url.wechat_basic_access_token,
            data={
                'grant_type': 'client_credential',
                'appid': appid,
                'secret': app_info['secret']
            })
    except tornado.httpclient.HTTPError:
        raise tornado.gen.Return({'err_code': 1001})
    result = _parse_wechat_resp(resp)
    if result['err_code'] != 0:
        raise tornado.gen.Return(result)
    else:
        result_data = result.get('data')
        wechat_storage.add_access_token(app_info['id'], appid, result_data.get('access_token'),
                                        result_data.get('expires_in'))
        raise tornado.gen.Return(result)
Beispiel #5
0
 def login(self, appid):
     appinfo = wechat_storage.get_app_info(appid)
     username = appinfo['mp_username']
     pwd = appinfo['mp_pwd']
     try:
         resp = yield self._post_form(appid, url.mp_login, {
             'username': username,
             'pwd': pwd,
             'f': 'json'})
     except tornado.httpclient.HTTPError:
         logging.warning('login failed: %s', appid)
         raise tornado.gen.Return({'err_code': 7000})
     result = _parse_mp_resp(resp)
     if result['err_code'] != 0:
         logging.warning('login failed: %s %s', appid, result)
         raise tornado.gen.Return(result)
     result_data = result.get('data')
     self.tokens[appid] = {
         'last_login': time.time(),
         'token': dict(urllib.parse.parse_qsl(result_data['redirect_url']))['token']
     }
     logging.info('login success: %s', appid)
     raise tornado.gen.Return({
         'err_code': 0,
         'data': {'token': self.tokens[appid]['token']}
     })
Beispiel #6
0
 def _init_cookies(self, appid):
     appinfo = wechat_storage.get_app_info(appid)
     self.cookies[appid] = {
         'data_bizuin': appinfo['data_bizuin'],
         'slave_user': appinfo['openid'],
         'bizuin': appinfo['fakeid']
     }