提问人:Jake Lund 提问时间:11/29/2022 最后编辑:Jake Lund 更新时间:11/29/2022 访问量:24
如何让快速应用程序从外部重定向 API 调用运行获取路由
How to have express app run a get route from external redirect api call
问:
我正在使用 MERN 应用程序授权我的 QuickBooks 帐户使用他们的 API 调用。
app.get("/api/quick-books/authorize", async (req, res, next) => {
try {
const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.Accounting],
state: "intuit-test",
});
res.send(authUri);
} catch (error) {
next(error);
}
});
app.get("/api/quick-books/authorize-callback", async (req, res, next) => {
try {
const parseRedirect = req.url;
oauthClient
.createToken(parseRedirect)
.then((authResponse) => {
oauthClient.setToken(authResponse.token);
const newQuickBookToken = new QuickBookToken({
realmId: authResponse.token.realmId,
refreshToken: authResponse.token.refresh_token,
});
newQuickBookToken.save();
return res.send("Connected to quick books please close this window");
})
.catch((e) => {
throw e;
});
} catch (error) {
next(error);
}
});
当我在localhost上执行此操作时,它完全按预期工作。但是当我在我的暂存环境(使用 Heroku)上时,在我连接到 QuickBooks 后,/api/quick-books/authorize-callback 路由永远不会被调用,而只是在根域 /
以下是我的服务器的设置方式
const path = require("path");
const express = require("express");
const cors = require("cors");
const db = require("./DB/conn");
db.on("error", console.error.bind(console, "MongoDB connection error:"));
const PORT = process.env.PORT || 5000;
const app = express();
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((req, res, next) => {
if (process.env.NODE_ENV === "production") {
if (req.headers["x-forwarded-proto"] !== "https")
return res.redirect("https://" + req.headers.host + req.url);
}
next();
});
require("./routes")(app);
app.use(express.static(path.resolve(__dirname, "../client/build")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "../client/build", "index.html"));
});
if (process.env.NODE_ENV !== "test")
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
module.exports = { app };
我厌倦了从 QuickBooks 重定向触发我的 get 路由,但它只有在本地计算机上运行时才有效。
答: 暂无答案
评论