【问题标题】:403 access denied error with Node.js fetch on external APINode.js 在外部 API 上获取 403 访问被拒绝错误
【发布时间】:2020-05-07 19:34:34
【问题描述】:

我已经在 Heroku 上部署了我的 Node.js Koa API 应用程序。我正在调用 USAJOB.gov API,它可以在我的本地主机上运行。但是,在 Heroku 上我收到此错误:

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

这是我的 Koa 路由器 sn-p:

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',
  });
}

【问题讨论】:

    标签: node.js api heroku koa node-fetch


    【解决方案1】:

    事实证明,是否应该使用凭据进行跨站点访问控制请求才是问题所在。我无法弄清楚如何使用 node-fetch 配置它,但是一旦我阅读了解决方案(下面的链接),切换到 axios,配置withCredentials: true,并添加headers: { 'X-Requested-With': 'XMLHttpRequest' },然后一切正常。

    来源:axios delete method gives 403

    【讨论】:

    • 在 node-fetch 上你需要credentials: 'include' 而不是withCredentials: true
    猜你喜欢
    • 2021-08-31
    • 2016-08-22
    • 2011-04-25
    • 2017-03-16
    • 1970-01-01
    • 2014-11-26
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多