不清楚,从你分享的代码来看,你用的是什么样的
fetch
。
您应该使用模板提供的获取:
import {useAuthenticatedFetch} from "./hooks/index.js";
const fetch = useAuthenticatedFetch();
我在这里报告以获得完整的答案:
export function useAuthenticatedFetch() {
const app = useAppBridge();
const fetchFunction = authenticatedFetch(app);
return async (uri, options) => {
const response = await fetchFunction(uri, options);
checkHeadersForReauthorization(response.headers, app);
return response;
};
}
function checkHeadersForReauthorization(headers, app) {
if (headers.get("X-Shopify-API-Request-Failure-Reauthorize") === "1") {
const authUrlHeader =
headers.get("X-Shopify-API-Request-Failure-Reauthorize-Url") ||
`/api/auth`;
const redirect = Redirect.create(app);
redirect.dispatch(
Redirect.Action.REMOTE,
authUrlHeader.startsWith("/")
? `https://${window.location.host}${authUrlHeader}`
: authUrlHeader
);
}
}