微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

部署到 heroku 时,对托管在 cloudflare 中的 Api 的获取请求返回 403 错误

如何解决部署到 heroku 时,对托管在 cloudflare 中的 Api 的获取请求返回 403 错误

我正在尝试使用 Cowin api (https://apisetu.gov.in/public/api/cowin) 来获取可用插槽。我正在使用 nodejs。当我在本地机器上运行它时,它工作正常,但在部署到 heroku 后出现以下错误

2021-05-09T11:07:00.862504+00:00 app[web.1]: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2021-05-09T11:07:00.862505+00:00 app[web.1]: <HTML><HEAD><Meta HTTP-EQUIV="Content-Type" 
CONTENT="text/html; charset=iso-8859-1">
2021-05-09T11:07:00.862506+00:00 app[web.1]: <TITLE>ERROR: The request Could not be 
satisfied</TITLE>
2021-05-09T11:07:00.862508+00:00 app[web.1]: </HEAD><BODY>
2021-05-09T11:07:00.862508+00:00 app[web.1]: <H1>403 ERROR</H1>
2021-05-09T11:07:00.862509+00:00 app[web.1]: <H2>The request Could not be satisfied.</H2>
2021-05-09T11:07:00.862509+00:00 app[web.1]: <HR noshade size="1px">
2021-05-09T11:07:00.862509+00:00 app[web.1]: Request blocked.
2021-05-09T11:07:00.862510+00:00 app[web.1]: We can't connect to the server for this app or 
website at this time. There might be too much traffic or a configuration error. Try again 
later,or contact the app or website owner.
2021-05-09T11:07:00.862513+00:00 app[web.1]: <BR clear="all"> 
2021-05-09T11:07:00.862514+00:00 app[web.1]: If you provide content to customers through 
CloudFront,you can find steps to troubleshoot and help prevent this error by reviewing the 
CloudFront documentation.
2021-05-09T11:07:00.862514+00:00 app[web.1]: <BR clear="all">
2021-05-09T11:07:00.862515+00:00 app[web.1]: <HR noshade size="1px">
2021-05-09T11:07:00.862515+00:00 app[web.1]: <PRE>
2021-05-09T11:07:00.862515+00:00 app[web.1]: Generated by cloudfront (CloudFront)
2021-05-09T11:07:00.862516+00:00 app[web.1]: Request ID: CW6sc_UgM9WJOFIvpk- 
ePGq7hVbYq8FuahgqPToRueh3PuLj35Q6mg==
2021-05-09T11:07:00.862517+00:00 app[web.1]: </PRE>
2021-05-09T11:07:00.862517+00:00 app[web.1]: <ADDRESS>
2021-05-09T11:07:00.862517+00:00 app[web.1]: </ADDRESS>
2021-05-09T11:07:00.862517+00:00 app[web.1]: </BODY></HTML> hello

这是我的源代码

const express = require('express')
var http = require('http'),httpProxy = require('http-proxy');
const { URL,URLSearchParams } = require('url');    
const app = express();
var proxy = httpProxy.createProxyServer({});
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var request = new XMLHttpRequest();



proxy.on('proxyReq',function(proxyReq,req,res,options) {
proxyReq.setHeader('X-Special-Proxy-Header','foobar');
 });

var PORT = process.env.PORT || 3006;

var cron = require('node-cron');

 const getMsg = () => {

var url_orig = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin'
var url = new URL(url_orig)

var params = {pincode:'226006',date:'10/5/2021'}
url.search = new URLSearchParams(params).toString();
request.open('GET',url,false)
console.log(url)
request.send();
console.log(request.responseText,'hello')
if (request.readyState == 4 && request.status == 200){
    
    return request.responseText
}
}

 cron.schedule('* * * * *',() => {
 
  fetchApi();
  });

 function fetchApi(){

var data = getMsg()
data = data.trim()
const g = JSON.parse(data) 

 for(var centre in g){
  for(var session in g[centre][0]['sessions']){
    let response = g[centre][0]['sessions'][session]
    console.log(response.min_age_limit);
           
   }
   }
   }


 var server = http.createServer(function(req,res) {

 proxy.web(req,{
  target: 'https://vaccines-notify.herokuapp.com'
 });
});


server.listen(PORT)

谁能帮我解决这个问题。 api 在 cloudflare 上很热。

解决方法

Cowin 公共 API 不适用于位于印度境外的数据中心。 Heroku 数据中心可能位于印度境外,因此您会收到此错误。您可以按照以下步骤检查IP地址和位置。

执行此命令以获取面向公众的 IP 地址(来自您的云实例)

curl ipinfo.io 

这将返回您的 IP 地址、位置等...

{
  "ip": "103.**.***.25","hostname": "******","city": "*****","region": "******","country": "IN","loc": "**,**","org": "*****","postal": "****","timezone": "Asia/Kolkata","readme": "https://ipinfo.io/missingauth"
}

您可以尝试在位于印度的数据中心部署您的应用程序。大多数流行的云提供商(例如 AWS、Google、IBM Cloud、Azure 等)都在印度设有数据中心。

我已在 Google Cloud(孟买 DC)中部署了我的 covin 应用程序。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。