【发布时间】:2021-10-13 08:08:07
【问题描述】:
我正在尝试将 Fauna 和 Auth0 集成到我的 Vue 3 应用程序中。
为了实现这一点,我关注this Auth0 guide 和这个youtube video。
简而言之,我已将 Auth0 配置为 Fauna 内部的提供程序。我将 Auth0 生成的 JWT 令牌作为 Fauna 密码发送。然后 Fauna 应解码 JWT 并授予对调用的访问权限。
为了测试它,我的代码从 Fauna 获取一些虚拟“产品”数据并将其打印到控制台。
但是当我拨打电话时,它会返回 unauthorized。
我做错了什么?
这是我的 Vue 组件中正在调用的脚本:
import { defineComponent, inject } from "vue";
import { query as q, Client } from "faunadb";
export default defineComponent({
name: "Api",
setup() {
let apiMessage = null;
let executed = false;
const auth = inject("Auth");
const callApi = async () => {
const accessToken = await auth.getTokenSilently();
console.log(accessToken);
try {
const client = new Client({ secret: accessToken });
const { Paginate, Documents, Collection } = q;
const data = await client.query(
Paginate(Documents(Collection("products")))
);
console.log(data);
apiMessage = data;
executed = true;
} catch (e) {
console.log(e);
apiMessage = `Error: the server responded with '${e.response.status}: ${e.response.statusText}'`;
}
};
return {
callApi,
};
},
});
这是返回的unauthorized 响应对象的副本:
{
"name": "Unauthorized",
"message": "unauthorized",
"description": "Unauthorized",
"requestResult": {
"method": "POST",
"path": "",
"query": null,
"requestRaw": "{\"paginate\":{\"documents\":{\"collection\":\"products\"}}}",
"requestContent": {
"raw": {
"paginate": {
"raw": {
"documents": {
"raw": {
"collection": "products"
}
}
}
}
}
},
"responseRaw": "{\"errors\":[{\"code\":\"unauthorized\",\"description\":\"Unauthorized\"}]}",
"responseContent": {
"errors": [
{
"code": "unauthorized",
"description": "Unauthorized"
}
]
},
"statusCode": 401,
"responseHeaders": {
"content-length": "65",
"content-type": "application/json;charset=utf-8",
"x-txn-time": "1634006015704445"
},
"startTime": 1634006014934,
"endTime": 1634006015885
}
}
【问题讨论】: