human
Complete this python program:
def check_apikey(kwargs, nokey=False):
assistant
#Here is the Python code you requested.

    """Check api key or nzbkey
    Return None when OK, otherwise an error message
    """
    def log_warning(txt):
        xff = cherrypy.request.headers.get('X-Forwarded-For')
        if xff:
            txt = ('%s    %s    (X-Forwarded-For:    %s)>%s' % (txt, cherrypy.request.remote.ip, xff, cherrypy.request.headers.get('User-Agent', '??')))
        else:
            txt = ('%s    %s>%s' % (txt, cherrypy.request.remote.ip, cherrypy.request.headers.get('User-Agent', '??')))
        logging.warning('%s', txt)
    output = kwargs.get('output')
    mode = kwargs.get('mode', '')
    name = kwargs.get('name', '')
    callback = kwargs.get('callback')
    req_access = sabnzbd.api.api_level(mode, name)
    if ((req_access == 1) and check_access(1)):
        pass
    elif (not check_access(req_access)):
        return report(output, 'Access    denied')
    if (not (cfg.disable_key() or nokey)):
        key = kwargs.get('apikey')
        if (not key):
            key = kwargs.get('session')
        if (not key):
            if cfg.api_warnings():
                log_warning(T('API    Key    missing,    please    enter    the    api    key    from    Config->General    into    your    3rd    party    program:'))
            return report(output, 'API    Key    Required', callback=callback)
        elif ((req_access == 1) and (key == cfg.nzb_key())):
            return None
        elif (key == cfg.api_key()):
            return None
        else:
            log_warning(T('API    Key    incorrect,    Use    the    api    key    from    Config->General    in    your    3rd    party    program:'))
            return report(output, 'API    Key    Incorrect', callback=callback)
    if (cfg.username() and cfg.password()):
        if (check_login() or ((kwargs.get('ma_username') == cfg.username()) and (kwargs.get('ma_password') == cfg.password()))):
            pass
        else:
            if cfg.api_warnings():
                log_warning(T('Authentication    missing,    please    enter    username/password    from    Config->General    into    your    3rd    party    program:'))
            return...