def get_user_info_by_token(openid, token): ''' get user-info by token and openid from WXServer Flow: HHS<->WXS e.g. https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID&lang=zh_CN ''' ### fetch data url_prefix = 'https://api.weixin.qq.com/sns/userinfo' paras = [('access_token', token), ('openid', openid), ('lang', 'zh_CN')] page = commutil.fetch_page_with_paras(url_prefix, paras) ### parse userinfo logging.debug('[AAA]') logging.debug(page) try: user_info = json.loads(page) except: logging.error('load json error') return None if user_info is None: logging.error('user_info is None') return None if not 'nickname' in user_info: #::notice:: other info check omitted logging.error('no nickname in user_info') return None return user_info
def get_user_token_by_code(code): ''' get user-access-token from WXServer Flow: HHS<->WXS ''' if code is None: return None ### fetch data # e.g. https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code url_prefix = 'https://api.weixin.qq.com/sns/oauth2/access_token' paras = [('appid', hxcfg.WX_APPID), ('secret', hxcfg.WX_APPSECRET), ('code', code), ('grant_type', 'authorization_code')] page = commutil.fetch_page_with_paras(url_prefix, paras) if page is None: return None ### parse code logging.debug('[AAA]') logging.debug(page) try: info = json.loads(page) except: logging.error('load json error') return None return info