Auth0 "service not found" 错误
Auth0 "service not found" error
我正在尝试使用 Auth0 颁发 JWT 令牌以访问我的 API(以便 Auth0 处理所有 OAuth 和安全问题等,而我的 API 只需要检查令牌)。当我尝试为客户端测试授权代码流以接收访问令牌(使用 Node + Express)时,会发生以下情况:
授权代码请求工作正常,客户端被重定向回我的 redirect_uri,代码附加到查询。一切顺利
令牌请求总是失败。如果我包含 audience
参数,请求 returns 一个 access_denied
错误,包含以下详细信息:Service not found: {the audience parameter}
,无论我为 audience
参数设置什么值.
如果我不包含 audience
参数,我会得到 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
。更正我的代码中的 URLs 解决了这个问题。
我的解决方案是找不到 api 的标识符。如果它不准确,它将找不到它。我的 'audience' 上有一个额外的反斜杠,其中标识符没有。很容易犯错误,但在 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 受众值
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
我正在尝试使用 Auth0 颁发 JWT 令牌以访问我的 API(以便 Auth0 处理所有 OAuth 和安全问题等,而我的 API 只需要检查令牌)。当我尝试为客户端测试授权代码流以接收访问令牌(使用 Node + Express)时,会发生以下情况:
授权代码请求工作正常,客户端被重定向回我的 redirect_uri,代码附加到查询。一切顺利
令牌请求总是失败。如果我包含
audience
参数,请求 returns 一个access_denied
错误,包含以下详细信息:Service not found: {the audience parameter}
,无论我为audience
参数设置什么值.如果我不包含
audience
参数,我会得到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
。更正我的代码中的 URLs 解决了这个问题。
我的解决方案是找不到 api 的标识符。如果它不准确,它将找不到它。我的 'audience' 上有一个额外的反斜杠,其中标识符没有。很容易犯错误,但在 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 受众值
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