[英]Auth0 "service not found" error
我正在尝试使用 Auth0 颁发 JWT 令牌以访问我的 API(以便 Auth0 处理所有 OAuth 和安全问题等,而我的 API 只需要检查令牌)。 当我尝试测试客户端的授权代码流以接收访问令牌(使用 Node + Express)时,会发生以下情况:
授权代码请求工作正常,客户端被重定向回我的 redirect_uri 并将代码附加到查询中。 都好。
然后令牌请求总是失败。 如果我包含audience
参数,请求将返回一个access_denied
错误,其中包含以下详细信息: Service not found: {the audience parameter}
,无论我为audience
参数设置了什么值。
如果我不包含audience
参数, server_error
收到server_error
消息,并显示Service not found: https://oauth.auth0.com/userinfo
。
我已经检查了每个 Auth0 设置并彻底阅读了每个文档页面,但到目前为止没有任何效果。 我还在 Auth0 的 API 调试器中测试了授权代码流程,它运行良好。 我的测试遵循完全相同的参数,但仍然收到请求令牌的错误。 我正在本地主机上测试。 客户端凭据和隐式流工作正常。
这是我创建的一个测试端点,它从 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
然后重定向到以下端点,我在那里请求访问令牌:
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`); };
关于我可能做错了什么的任何建议?
问题是我在 Auth0 调用了错误的 URL。 我错误地认为授权和令牌端点都以/oauth
开头,而实际上授权端点只是/authorize
,而令牌端点是/oauth/authorize
。 更正我的代码中的 URL 解决了这个问题。
我的解决方案是找不到api的标识符。 如果它不准确,它就不会找到它。 我在我的“观众”上有一个额外的反斜杠,其中标识符没有。 很容易出错,但错误在 Auth0 中不是很清楚。
就我而言,我使用的是 auth0 反应钩子。 所以示例代码如下所示:
const getUserMetadata = async () => {
const domain = process.env.REACT_APP_AUTH0_DOMAIN
try {
const accessToken = await getAccessTokenSilently({
audience: `https://${domain}/api/v2/`,
scope: 'read:current_user',
})
console.log('accessToken', accessToken)
localStorage.setItem('access_token', accessToken)
setUserAuthenticated(true)
} catch (e) {
console.log('error in getting access token', e.message)
}
}
我对此的解决方案是在audience
字段中默认使用 Auth0 Audience 值
const getUserMetadata = async () => {
const auth0audience = process.env.REACT_APP_AUTH0_AUDIENCE
try {
const accessToken = await getAccessTokenSilently({
audience: auth0audience,
scope: 'read:current_user',
})
console.log('accessToken', accessToken)
localStorage.setItem('access_token', accessToken)
setUserAuthenticated(true)
} catch (e) {
console.log('error in getting access token', e.message)
}
}
因为它在配置自定义域的 auth0 文档中说明,您需要默认使用 API 受众
来源 - https://auth0.com/docs/brand-and-customize/custom-domains/configure-features-to-use-custom-domains
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.