Пример #1
0
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 "获取令牌失败"
Пример #2
0
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 "获取令牌失败"