/api/v2/users/{id} 缺少身份验证 Auth0
Missing Authentication Auth0 for /api/v2/users/{id}
我已经从到达终点 /oauth/token
获得了令牌,并通过邮递员将其传递给 /api/v2/users/{id}
,就像这样
{Authorization: `Bearer ${token}`}
在邮递员上它工作得很好,并把用户还给了我。但是,当我在我的应用程序中执行此操作时,我返回了 401 错误。我正在使用 axios 并像这样传递令牌。
return new Promise((resolve, reject) => {
axios
.post('https://taustin.auth0.com/oauth/token', body, headers)
.then(res => {
resolve(res.data.access_token);
})
.catch(err => {
reject(err);
});
});
以上是generateToken函数。
generateToken()
.then(token => {
const headers = { Authorization: `Bearer ${token}` };
console.log(token); //token is valid here
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers
)
.then(res => {
console.log(res);
});
})
在 catch 块中,我收到 401 unauth 错误和消息。我不确定我在做什么与邮递员和我的应用程序有什么不同,但我做错了什么。
你没有通过 headers 你应该
你应该这样做:
const headers = { Authorization: `Bearer ${token}` };
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers: headers
).then().catch()
或者这样尝试
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers: {`Authorization: `Bearer ${token}`}
).then().catch()
希望这些对您有所帮助,如果您需要帮助,请回复。
如果您不确定是否正确设置了 Axios,请查看 this react sample(几个月前写的,但应该仍然相关)。
如果您的问题不在于您如何塑造 Axios 请求,而是在于其他方面,请检查访问令牌。例如,实际使用您在应用程序中使用的访问令牌,并通过 Postman 尝试使用相同的访问令牌。如果您的访问令牌不是 opaque
,而是 JWT 访问令牌,请检查 audience
值。它应该符合 https://taustin.auth0.com/userinfo/
.
最后,如果 postman 正常工作,您还可以尝试生成代码片段选项,并使用 request
(node.js) 导出工作示例。然后,如果可行,请沿相同的行修改为 Axios。欢迎在下面的评论中提出 post 个问题。
我已经从到达终点 /oauth/token
获得了令牌,并通过邮递员将其传递给 /api/v2/users/{id}
,就像这样
{Authorization: `Bearer ${token}`}
在邮递员上它工作得很好,并把用户还给了我。但是,当我在我的应用程序中执行此操作时,我返回了 401 错误。我正在使用 axios 并像这样传递令牌。
return new Promise((resolve, reject) => {
axios
.post('https://taustin.auth0.com/oauth/token', body, headers)
.then(res => {
resolve(res.data.access_token);
})
.catch(err => {
reject(err);
});
});
以上是generateToken函数。
generateToken()
.then(token => {
const headers = { Authorization: `Bearer ${token}` };
console.log(token); //token is valid here
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers
)
.then(res => {
console.log(res);
});
})
在 catch 块中,我收到 401 unauth 错误和消息。我不确定我在做什么与邮递员和我的应用程序有什么不同,但我做错了什么。
你没有通过 headers 你应该
你应该这样做:
const headers = { Authorization: `Bearer ${token}` };
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers: headers
).then().catch()
或者这样尝试
axios
.get(
`https://taustin.auth0.com/api/v2/users/${profile.sub}`,
headers: {`Authorization: `Bearer ${token}`}
).then().catch()
希望这些对您有所帮助,如果您需要帮助,请回复。
如果您不确定是否正确设置了 Axios,请查看 this react sample(几个月前写的,但应该仍然相关)。
如果您的问题不在于您如何塑造 Axios 请求,而是在于其他方面,请检查访问令牌。例如,实际使用您在应用程序中使用的访问令牌,并通过 Postman 尝试使用相同的访问令牌。如果您的访问令牌不是 opaque
,而是 JWT 访问令牌,请检查 audience
值。它应该符合 https://taustin.auth0.com/userinfo/
.
最后,如果 postman 正常工作,您还可以尝试生成代码片段选项,并使用 request
(node.js) 导出工作示例。然后,如果可行,请沿相同的行修改为 Axios。欢迎在下面的评论中提出 post 个问题。