'Return token or error in json format.
If the access token request is valid and authorized, the
authorization server issues an access token and optional refresh
token as described in `Section 5.1`_.  If the request failed client
authentication or is invalid, the authorization server returns an
error response as described in `Section 5.2`_.
.. _`Section 5.1`: http://tools.ietf.org/html/rfc6749#section-5.1
.. _`Section 5.2`: http://tools.ietf.org/html/rfc6749#section-5.2'

def create_token_response(self, request, token_handler):

    headers = {u'Content-Type': u'application/json', u'Cache-Control': u'no-store', u'Pragma': u'no-cache'}
    try:
        if self.request_validator.client_authentication_required(request):
            log.debug(u'Authenticating    client,    %r.', request)
            if (not self.request_validator.authenticate_client(request)):
                log.debug(u'Client    authentication    failed,    %r.', request)
                raise errors.InvalidClientError(request=request)
        elif (not self.request_validator.authenticate_client_id(request.client_id, request)):
            log.debug(u'Client    authentication    failed,    %r.', request)
            raise errors.InvalidClientError(request=request)
        log.debug(u'Validating    access    token    request,    %r.', request)
        self.validate_token_request(request)
    except errors.OAuth2Error as e:
        log.debug(u'Client    error    in    token    request,    %s.', e)
        return (headers, e.json, e.status_code)
    token = token_handler.create_token(request, self.refresh_token)
    log.debug(u'Issuing    token    %r    to    client    id    %r    (%r)    and    username    %s.', token, request.client_id, request.client, request.username)
    return (headers, json.dumps(token), 200)
