简体   繁体   English

在外部 API 上使用 Node.js 获取 403 访问被拒绝错误

[英]403 access denied error with Node.js fetch on external API

I've deployed my Node.js Koa API app on Heroku.我已经在 Heroku 上部署了我的 Node.js Koa API 应用程序。 I'm making a call to a USAJOB.gov API and it works on my localhost.我正在调用 USAJOB.gov API,它在我的本地主机上工作。 However, on Heroku I get this error:但是,在 Heroku 上,我收到此错误:

You don't have permission to access "http://data.usajobs.gov/$(SERVE_403)/api/search?" on this server.

Here is my Koa router snippet:这是我的 Koa 路由器片段:

router.get('/jobs', async (ctx) => {
  const { keyword, location } = ctx.query;
  const url = `https://data.usajobs.gov/api/search? 
    Keyword=${keyword}&LocationName=${location}`;
  const host = 'data.usajobs.gov';
  const userAgent = '<redacted>';
  const authKey = '<redacted>';
  const headers = {
    Host: host,
    'User-Agent': userAgent,
    'Authorization-Key': authKey,
  };

  await fetch(url, {
  headers,
  method: 'GET',
  });
}

So it turns out that whether or not cross-site Access-Control requests should be made using credentials was what was the issue.因此,问题是是否应该使用凭据进行跨站点访问控制请求 I wasn't able to figure out how to configure that with node-fetch but once I read the the solution (link below), switched to axios, configured withCredentials: true , and added headers: { 'X-Requested-With': 'XMLHttpRequest' } , then it all worked.我无法弄清楚如何使用 node-fetch 配置它,但是一旦我阅读了解决方案(下面的链接),切换到 axios,配置了withCredentials: true ,并添加了headers: { 'X-Requested-With': 'XMLHttpRequest' } ,然后一切正常。

Source: axios delete method gives 403来源: axios删除方法给出403

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM