import requests


def _get_token(ZC, username, password):
    """Gets the client's token using a username and password."""

    url = '{}/oauth/token/'.format(ZC.HOME)
    data = {'username': username, 'password': password, 'noexpire': True}

    r = requests.post(url, json=data)

    if r.status_code != 200:
        raise Exception(f'Couldn\'t set token, bad response ({r.status_code})'
                        '\nWas your username/password correct?')

    j = r.json()

    return j['token']