public static function get_token($request) {
        // try to get the token from request object
        $tokenStrFromHeader = $request->getHeader('Authorization');
        $tokenStrFromVar = $request->requestVar('access_token');
        if (!empty($tokenStrFromHeader))  {
            // string must have format: type token
            return explode(' ', $tokenStrFromHeader)[1];
        } else if(!empty($tokenStrFromVar)) {
            // try variables
            return $tokenStrFromVar;
        } else if(function_exists('getallheaders')) {
            // get all headers from apache server
            $headers = getallheaders();
            if(isset($headers['Authorization'])) {
                return explode(' ', $headers['Authorization'])[1];
            }
        }
        throw new \Exception("Token can't be read or was not specified");
    }