简体   繁体   English

"从 node express 应用程序向需要自己身份验证的第三方 api 发出请求的最佳方法是什么?"

[英]What is the best way to make a request from a node express app to a third party api that requires its own authentication?

I have a node express app that uses keycloak authentication to secure all API endpoints.我有一个 node express 应用程序,它使用 keycloak 身份验证来保护所有 API 端点。 Express middleware has been set up for authentication to make sure that each incoming request from the front end has the appropriate keycloak token. Express 中间件已设置用于身份验证,以确保来自前端的每个传入请求都具有适当的 keycloak 令牌。 I need to make a post request from my node app to a third party backend API to subscribe users to an email service that uses a different authentication method which my middleware would not work with.我需要从我的节点应用程序向第三方后端 API 发出发布请求,以便为用户订阅电子邮件服务,该服务使用我的中间件无法使用的不同身份验证方法。

What would be the best practice for making a request from the third party API?从第三方 API 发出请求的最佳实践是什么? I am considering creating a new express instance and use a separate middleware specific for that post request .我正在考虑创建一个新的 express 实例,并为该 post 请求使用一个单独的中间件 Is this an ok thing to do or is there a better way?这是一件好事还是有更好的方法?

Here is a simplified version of my node app.这是我的节点应用程序的简化版本。 See the

index.js index.js

import { authmware } from "./authmware";
import express from "express";
import { router } from "./router";

const app = express();
authmware(app);
router(app);

app.use((err, req, res, next) => {
  logger.error(err.message);
  const code = err.code ? err.code : 500;
  const message = err.message ? err.message : "Internal Server Error";

  res.status(code).json({ error: message, success: false });
});

export default app;

router.js路由器.js

import express from "express";
import createProfile from "../../controllers/createProfile";
    
const router = express.Router();
    
router.post("/", createProfile);
    
export const router = (app) => {
   app.use("/api/v1/createProfile", router);
};

controllers/createProfile.js控制器/createProfile.js

const createProfile = async (req, res) => {

  // ... Do some stuff

  // ** make request to a different api here ** 
  await makeThirdPartyApiRequest();

}

How would I make this third party api request that uses a different style of authentication?我将如何发出使用不同身份验证方式的第三方 api 请求?

This is a very common use case.这是一个非常常见的用例。 You can use 10 third party APIs in your node server and all having different authentication mechanisms irrespective of the auth you are using for your client requests.您可以在您的节点服务器中使用 10 个第三方 API,它们都具有不同的身份验证机制,而与您用于客户端请求的身份验证无关。

await makeThirdPartyApiRequest();
  // http request to API
  // attach proper auth headers (API key / jwt / basic auth / oAuth token). This will be based on the authentication method the API is offering.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Node获取对第三方API的请求 - GET request to a third party API using Node 使用 express.router() 使用 api 密钥向第三方 API 发出请求; - Make get request to third party API with api key using express.router(); 如果向第三方 API 发出错误请求,Node JS Express 如何发送 404 错误? - Node JS Express how can I send a 404 error if a bad request is made to third party API? 使用 Node.js 监控第三方流量的最佳方式是什么? - What's the best way to monitor third-party traffic with Node.js? 没有从我的第三方得到响应 object API 呼叫快递路线 node.js - Not getting a response object from my third party API call express routes node.js 从第三方网站检索Cookie /会话内容的最佳方法是什么? - What is best way to retrieve cookie/session contents from third party website? 从第三方站点的script / include标记配置Javascript的最佳方法是什么? - What is the best way to configure Javascript from third-party site's script/include tag? Node.js +向第三方服务发送API请求 - Node.js + Sending API Request to Third Party Service 如何从反应到节点/表达 api 发出发布请求 - How To make a post request from react to node/express api 如何向需要身份验证的API发出Javascript JSON请求 - How to make a Javascript JSON Request to an API that requires authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM