def get_access_token(rds, db, wx_appid): access_token = WX.get_access_token(rds, wx_appid) if not access_token: app = App.get_wx(db, wx_appid) if not app: return None refresh_token = app['refresh_token'] component_token = get_component_access_token(rds) if not component_token: return None wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.refresh_auth(wx_appid, refresh_token) if r.get('errcode'): logging.error("refresh auto error:%s %s", r['errcode'], r['errmsg']) return None token = r['authorizer_access_token'] expires = r['expires_in'] authorizer_refresh_token = r['authorizer_refresh_token'] #提前10分钟过期 if expires > 20 * 60: expires = expires - 10 * 60 if authorizer_refresh_token != refresh_token: logging.error("old refresh token:%s new refresh token:%s", refresh_token, authorizer_refresh_token) else: logging.debug("refresh token is unchanged") WX.set_access_token(rds, wx_appid, token, expires) access_token = token return access_token
def get_access_token(rds, db, wx_appid): access_token = WX.get_access_token(rds, wx_appid) if not access_token: app = App.get_wx(db, wx_appid) if not app: return None refresh_token = app['refresh_token'] component_token = get_component_access_token(rds) if not component_token: return None wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.refresh_auth(wx_appid, refresh_token) if r.get('errcode'): logging.error("refresh auto error:%s %s", r['errcode'], r['errmsg']) return None token = r['authorizer_access_token'] expires = r['expires_in'] authorizer_refresh_token = r['authorizer_refresh_token'] #提前10分钟过期 if expires > 20*60: expires = expires - 10*60 if authorizer_refresh_token != refresh_token: logging.error("old refresh token:%s new refresh token:%s", refresh_token, authorizer_refresh_token) else: logging.debug("refresh token is unchanged") WX.set_access_token(rds, wx_appid, token, expires) access_token = token return access_token
def handle_authorized(data): authorizer_appid = data.get('AuthorizerAppid') authorization_code = data.get('AuthorizationCode') code_expire = data.get('AuthorizationCodeExpiredTime') logging.debug("authorized appid:%s code:%s expire:%s", authorizer_appid, authorization_code, code_expire) rds = g.im_rds db = g._db auth_code = authorization_code store_id = 0 component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20 * 60: expires_in = expires_in - 10 * 60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"
def auth_callback(uid): rds = g.im_rds db = g._db auth_code = request.args.get('auth_code') expires_in = request.args.get('expires_in') if not auth_code or not expires_in: return "非法调用" seller = Seller.get_seller(db, uid) store_id = seller['store_id'] logging.debug("auth callback code:%s uid:%s store_id:%s", auth_code, uid, store_id) component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] logging.debug("auth callback info:%s", info) wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20 * 60: expires_in = expires_in - 10 * 60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" if app['store_id'] == 0: App.set_store_id(db, app['id'], store_id) else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"
def handle_authorized(data): authorizer_appid = data.get('AuthorizerAppid') authorization_code = data.get('AuthorizationCode') code_expire = data.get('AuthorizationCodeExpiredTime') logging.debug("authorized appid:%s code:%s expire:%s", authorizer_appid, authorization_code, code_expire) rds = g.im_rds db = g._db auth_code = authorization_code store_id = 0 component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20*60: expires_in = expires_in - 10*60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"
def auth_callback(uid): rds = g.im_rds db = g._db auth_code = request.args.get('auth_code') expires_in = request.args.get('expires_in') if not auth_code or not expires_in: return "非法调用" seller = Seller.get_seller(db, uid) store_id = seller['store_id'] logging.debug("auth callback code:%s uid:%s store_id:%s", auth_code, uid, store_id) component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] logging.debug("auth callback info:%s", info) wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20*60: expires_in = expires_in - 10*60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" if app['store_id'] == 0: App.set_store_id(db, app['id'], store_id) else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"