【发布时间】:2021-12-23 20:13:24
【问题描述】:
我不知道如何在 fetcher 中将不记名令牌标头添加到我的 API 调用中以接收数据,因为我正在后端检查 JWT 令牌...
提取码:
const fetcher = async () => {
const response = await fetch(api); //I need to add bearer token headers to this api request or else it's 401
const data = await response.json();
const props = {
data: data
};
return props;
};
这是数据获取代码
const {data, error} = useSWR("data", fetcher);
if (error) return <div>oops... {error.message}</div>;
if (data === undefined) return <div>Loading...</div>;
console.log(accessToken);
return <div>{data}</div>;
我在一篇文章中发现了一段代码,说我需要添加这个:
const {accessToken} = getAccessToken({
scopes: ["read:shows"], // I don't know what is this scope I just pasted it...
});
但 accessToken 返回错误,只能在服务器端检索它...
【问题讨论】: