2017-04-16 68 views
0

我正在尝试使用Auth0发布JWT令牌以访问我的API(以便Auth0处理所有OAuth和安全问题等,而我的API只需检查令牌) 。当我尝试测试授权码流为客户收到一个访问令牌(使用节点+快递),会发生以下情况:Auth0“找不到服务”错误

  • 授权码请求工作正常,并在客户端被重定向回我的REDIRECT_URI将代码附加到查询中。都好。

  • 令牌请求然后总是失败。如果我包含audience参数,则请求将返回一个access_denied错误,并提供以下详细信息:Service not found: {the audience parameter},无论我为audience参数设置了什么值。

  • 如果我不包含audience参数,我会得到一个server_error,其中包含消息Service not found: https://oauth.auth0.com/userinfo

我检查了每个Auth0设置并彻底阅读每个文档页面,到目前为止没有任何工作。我还测试了Auth0的API调试器中的授权码流,并且它工作正常。我的测试完全遵循相同的参数,但仍然收到请求令牌的错误。我正在测试localhost。客户端证书和隐式流程工作正常。

下面是测试端点我创建了检索来自Auth0授权代码:

const qs = require('querystring'); 
 

 
const getCode = (req, res) => { 
 
    const params = { 
 
    audience,       // the value of the API Audience setting for the client 
 
    client_id,       // the client ID 
 
    redirect_uri,      // the redirect_uri, which is also listed in the Allowed Callback URLs field 
 
    response_type: `code`, 
 
    scope:   `offline_access open` // ask to return ID token and refresh token, 
 
    state:   `12345`, 
 
    }; 
 

 
    const authDomain = `mydomain.auth0.com/oauth`; 
 
    
 
    res.redirect(`${authDomain}/oauth/authorize?${qs.stringify(params)}`); 
 

 
};

redirect_uri然后重定向到以下端点,其中,I使所述接入令牌的请求:

const https = require('https'); 
 

 
const callback = (req, res) => { 
 

 
    const body = { 
 
    client_id, 
 
    client_secret, 
 
    code:   req.query.code, 
 
    grant_type: `authorization_code`, 
 
    redirect_uri, // same value as provided during the code request 
 
    }; 
 
    
 
    const opts = { 
 
    headers: { 'Content-Type': `application/json` }, 
 
    hostname: `mydomain.auth0.com`, 
 
    method: `POST`, 
 
    path:  `/oauth/token`, 
 
    }; 
 
    
 
    const request = https.request(opts, response => { 
 
    let data = ``; 
 
    response.on(`data`, chunk => { data += chunk; }); 
 
    response.on(`error`, res.send(err.message)); 
 
    response.on(`end`,() => res.json(JSON.parse(data))); // this executes, but displays the error returned from Auth0 
 
    }); 
 
    
 
    request.on(`error`, err => res.send(err.message)); 
 
    request.end(JSON.stringify(body), `utf8`); 
 

 
};

任何有关我可能会做错什么的建议?

+2

实际上包含您的代码可能是一个好主意。否则,这只是猜测。 –

+0

我对'https.request'不熟悉......但你为什么要把请求的主体串起来? –

+0

@MariaInesParnisari内容类型是application/json,因此主体必须采用正确的JSON字符串格式。如果我省略了代码中的那一步,请求就会失败,并执行'request.end(body)'。 – dwhieb

回答

3

问题是我在Auth0调用了错误的URL。我错误地认为授权和令牌端点始于/oauth,实际上授权端点只是/authorize,而令牌端点是/oauth/authorize。更正我的代码中的URL可以解决问题。

相关问题