Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在谷歌应用程序脚本中请求API数据时,是否有任何方法可以使用特定的IP?_Javascript_Api_Google Apps Script - Fatal编程技术网

Javascript 在谷歌应用程序脚本中请求API数据时,是否有任何方法可以使用特定的IP?

Javascript 在谷歌应用程序脚本中请求API数据时,是否有任何方法可以使用特定的IP?,javascript,api,google-apps-script,Javascript,Api,Google Apps Script,我正在使用Google应用程序脚本从网站请求API数据,然后将收到的数据记录在Google表单中。我遇到的问题是,我请求数据的网站需要两样东西:我的身份验证密钥和特定(静态)IP地址。它会自动记录发出请求的IP地址,如果我使用了错误的IP地址,它不会让我获取数据。不幸的是,Google应用程序脚本API请求使用了一系列动态IP,我最多只能允许5个不同的IP使用我的身份验证密钥。有没有办法指定我的谷歌应用程序脚本将使用哪个IP地址发送我的API请求 下面你可以看到我的代码;当IP地址与我在网站上写

我正在使用Google应用程序脚本从网站请求API数据,然后将收到的数据记录在Google表单中。我遇到的问题是,我请求数据的网站需要两样东西:我的身份验证密钥和特定(静态)IP地址。它会自动记录发出请求的IP地址,如果我使用了错误的IP地址,它不会让我获取数据。不幸的是,Google应用程序脚本API请求使用了一系列动态IP,我最多只能允许5个不同的IP使用我的身份验证密钥。有没有办法指定我的谷歌应用程序脚本将使用哪个IP地址发送我的API请求

下面你可以看到我的代码;当IP地址与我在网站上写的内容匹配时,这一切都非常有效,但如果它使用未知的IP地址,则返回未定义的

function fetchAPI(tag) {
  var url = 'https://myurl'
    + tag;
  var token = {'muteHttpExceptions': true};
  token.headers = {'Accept': 'application/json','authorization': 'my auth key'};
  var response = UrlFetchApp.fetch(url, token);
  return JSON.parse(response);
}
我想到的另一个选择是创建多个API密钥,每5个IP地址创建一个,但问题是Google的IP范围覆盖了数百个不同的地址:

如果您能想到任何可能有帮助的事情,我们将不胜感激!或者,如果你能想出一种方法,通过一个静态IP地址代理我的请求(而不必花钱来主持我自己的网站),请让我知道

根据这一点,作者观察到谷歌脚本中的ip使用模式:

有两个明显的观察结果:

  • IP可能在一个触发器内的连续呼叫中发生变化
  • IP之间存在明显的轮换,有时使用相同的IP连续呼叫7次,有时为6次。但总的来说 总是有团体
因此,可以使用反复调用服务器直到成功的解决方案:

function batchRequest(userLogin, userPassword, webapiKey, resource, attributes, values ){

    var token = requestToken(userLogin, userPassword, webapiKey );   // requestToken method uses UrlFetchApp.fetch
     var result = request(resource, token, attributes, values); 

    /** requestToken method uses UrlFetchApp.fetch with options.muteHttpExceptions set to true so that we can read the response code **/

     var i = 0;
     while (result.getResponseCode() == 500 && i < 10){
     token = requestToken(userLogin, userPassword, webapiKey ); //  requestToken method uses UrlFetchApp.fetch
     result = request(resource, token, attributes, values);
     i++;
     }
     return result;
    }
函数batchRequest(userLogin、userPassword、webapiKey、资源、属性、值){
var-token=requestToken(userLogin、userPassword、webapiKey);//requestToken方法使用UrlFetchApp.fetch
var结果=请求(资源、令牌、属性、值);
/**requestToken方法使用UrlFetchApp.fetch,并将options.muteHttpExceptions设置为true,以便读取响应代码**/
var i=0;
while(result.getResponseCode()==500&&i<10){
token=requestToken(userLogin、userPassword、webapiKey);//requestToken方法使用UrlFetchApp.fetch
结果=请求(资源、令牌、属性、值);
i++;
}
返回结果;
}
您可以根据最佳结果更改迭代次数