Google apps script UrlFetchApp.fetch中缺少响应标头

Google apps script UrlFetchApp.fetch中缺少响应标头,google-apps-script,http-headers,shopify,urlfetch,Google Apps Script,Http Headers,Shopify,Urlfetch,我目前在通过GoogleAppScript使用API时遇到了一个问题 我能够查询API并获取结果,但我需要捕获一个特定的头(链接),其中包含一个在第二次查询中使用的键 我使用的是来自的getAllHeaders()方法,但显然缺少目标标头 你对如何得到这个标题有什么想法吗 这是我的密码: function callShopify (action, productId, secondId) { var apiInfo = getShopifyFunction(action); var b

我目前在通过GoogleAppScript使用API时遇到了一个问题

我能够查询API并获取结果,但我需要捕获一个特定的头(链接),其中包含一个在第二次查询中使用的键

我使用的是来自的getAllHeaders()方法,但显然缺少目标标头

你对如何得到这个标题有什么想法吗

这是我的密码:

function callShopify (action, productId, secondId) {

  var apiInfo = getShopifyFunction(action);
  var baseApiUrl = apiInfo[0];
  var functionUrl = apiInfo[1];
  var extension = apiInfo[2];

  var header = {'X-Shopify-Access-Token': getShopifyApiPass()};
  var options = {
    'method' : apiInfo[3],
    'headers' : header,
    'muteHttpExceptions' : true
  };

  var finalUrl = getShopifyWebsite() + baseApiUrl + productId + functionUrl + secondId + extension+"?limit=250";
  var lock = LockService.getScriptLock();
  lock.waitLock(1000); // lock 1 second
  var response = UrlFetchApp.fetch(finalUrl, options);
  var test = response.getAllHeaders();
  Logger.log(JSON.stringify(response.getAllHeaders())); 
  Logger.log(JSON.stringify(response.getHeaders()));
  lock.releaseLock();
  return (JSON.parse(response));
}
以下是AppScript方法返回的标题:

{
   "Date":"Fri, 13 Mar 2020 18:36:47 GMT",
   "alt-svc":"h3-27=\":443\"; ma=86400, h3-25=\":443\"; ma=86400, h3-24=\":443\"; ma=86400, h3-23=\":443\"; ma=86400",
   "Connection":"keep-alive",
   "x-sorting-hat-podid":"96",
   "X-Download-Options":"noopen",
   "x-shardid":"96",
   "x-sorting-hat-shopid":"10366451809",
   "Referrer-Policy":"origin-when-cross-origin",
   "x-shopify-api-version":"2020-01",
   "x-shopid":"10366451809",
   "X-XSS-Protection":"1; mode=block; report=/xss-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "X-Content-Type-Options":"nosniff",
   "x-shopify-stage":"production",
   "Vary":"Accept-Encoding",
   "Content-Encoding":"gzip",
   "Content-Security-Policy":"default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopify.cn https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "x-shopify-api-terms":"By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms",
   "Strict-Transport-Security":"max-age=7889238",
   "cf-cache-status":"DYNAMIC",
   "x-permitted-cross-domain-policies":"none",
   "expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
   "x-shopify-shop-api-call-limit":"1/40",
   "x-stats-apipermissionid":"203626283105",
   "Report-To":[
      "{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}",
      "{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}"
   ],
   "Content-Type":"application/json; charset=utf-8",
   "x-stats-userid":"",
   "Transfer-Encoding":"chunked",
   "x-dc":"gcp-us-east1,gcp-us-central1,gcp-us-central1",
   "x-request-id":"86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "Server":"cloudflare",
   "http_x_shopify_shop_api_call_limit":"1/40",
   "cf-ray":"5737dcc7198ff381-ATL",
   "Set-Cookie":"__cfduid=d9afe6e8299af1521516fd6a8cfa91f271584124606; expires=Sun, 12-Apr-20 18:36:46 GMT; path=/; domain=.myshopify.com; HttpOnly; SameSite=Lax",
   "X-Frame-Options":"DENY",
   "x-stats-apiclientid":"3403993",
   "nel":[
      "{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}",
      "{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}"
   ]
}[
   20-03-13 19:   36:   47:   382 CET
]{
   "x-stats-apiclientid":"3403993",
   "nel":"{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}",
   "Date":"Fri, 13 Mar 2020 18:36:47 GMT",
   "alt-svc":"h3-27=\":443\"; ma=86400, h3-25=\":443\"; ma=86400, h3-24=\":443\"; ma=86400, h3-23=\":443\"; ma=86400",
   "Connection":"keep-alive",
   "x-sorting-hat-podid":"96",
   "X-Download-Options":"noopen",
   "x-shardid":"96",
   "x-sorting-hat-shopid":"10366451809",
   "Referrer-Policy":"origin-when-cross-origin",
   "x-shopify-api-version":"2020-01",
   "x-shopid":"10366451809",
   "X-XSS-Protection":"1; mode=block; report=/xss-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "X-Content-Type-Options":"nosniff",
   "x-shopify-stage":"production",
   "Vary":"Accept-Encoding",
   "Content-Encoding":"gzip",
   "Content-Security-Policy":"default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopify.cn https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "x-shopify-api-terms":"By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms",
   "Strict-Transport-Security":"max-age=7889238",
   "cf-cache-status":"DYNAMIC",
   "x-permitted-cross-domain-policies":"none",
   "expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
   "x-shopify-shop-api-call-limit":"1/40",
   "x-stats-apipermissionid":"203626283105",
   "Report-To":"{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}",
   "Content-Type":"application/json; charset=utf-8",
   "x-stats-userid":"",
   "Transfer-Encoding":"chunked",
   "x-dc":"gcp-us-east1,gcp-us-central1,gcp-us-central1",
   "x-request-id":"86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "Server":"cloudflare",
   "http_x_shopify_shop_api_call_limit":"1/40",
   "cf-ray":"5737dcc7198ff381-ATL",
   "Set-Cookie":"__cfduid=d9afe6e8299af1521516fd6a8cfa91f271584124606; expires=Sun, 12-Apr-20 18:36:46 GMT; path=/; domain=.myshopify.com; HttpOnly; SameSite=Lax",
   "X-Frame-Options":"DENY"
}

如您所见,“链接”标题不存在

为了确保问题来自AppScript,我还从Chrome Inspector检查了以下内容:

GetAllHeader和getHeader方法的JSON结果:

{
   "Date":"Fri, 13 Mar 2020 18:36:47 GMT",
   "alt-svc":"h3-27=\":443\"; ma=86400, h3-25=\":443\"; ma=86400, h3-24=\":443\"; ma=86400, h3-23=\":443\"; ma=86400",
   "Connection":"keep-alive",
   "x-sorting-hat-podid":"96",
   "X-Download-Options":"noopen",
   "x-shardid":"96",
   "x-sorting-hat-shopid":"10366451809",
   "Referrer-Policy":"origin-when-cross-origin",
   "x-shopify-api-version":"2020-01",
   "x-shopid":"10366451809",
   "X-XSS-Protection":"1; mode=block; report=/xss-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "X-Content-Type-Options":"nosniff",
   "x-shopify-stage":"production",
   "Vary":"Accept-Encoding",
   "Content-Encoding":"gzip",
   "Content-Security-Policy":"default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopify.cn https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "x-shopify-api-terms":"By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms",
   "Strict-Transport-Security":"max-age=7889238",
   "cf-cache-status":"DYNAMIC",
   "x-permitted-cross-domain-policies":"none",
   "expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
   "x-shopify-shop-api-call-limit":"1/40",
   "x-stats-apipermissionid":"203626283105",
   "Report-To":[
      "{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}",
      "{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}"
   ],
   "Content-Type":"application/json; charset=utf-8",
   "x-stats-userid":"",
   "Transfer-Encoding":"chunked",
   "x-dc":"gcp-us-east1,gcp-us-central1,gcp-us-central1",
   "x-request-id":"86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "Server":"cloudflare",
   "http_x_shopify_shop_api_call_limit":"1/40",
   "cf-ray":"5737dcc7198ff381-ATL",
   "Set-Cookie":"__cfduid=d9afe6e8299af1521516fd6a8cfa91f271584124606; expires=Sun, 12-Apr-20 18:36:46 GMT; path=/; domain=.myshopify.com; HttpOnly; SameSite=Lax",
   "X-Frame-Options":"DENY",
   "x-stats-apiclientid":"3403993",
   "nel":[
      "{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}",
      "{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}"
   ]
}[
   20-03-13 19:   36:   47:   382 CET
]{
   "x-stats-apiclientid":"3403993",
   "nel":"{\"report_to\":\"network-errors\",\"max_age\":2592000,\"failure_fraction\":0.01,\"success_fraction\":0.0001}",
   "Date":"Fri, 13 Mar 2020 18:36:47 GMT",
   "alt-svc":"h3-27=\":443\"; ma=86400, h3-25=\":443\"; ma=86400, h3-24=\":443\"; ma=86400, h3-23=\":443\"; ma=86400",
   "Connection":"keep-alive",
   "x-sorting-hat-podid":"96",
   "X-Download-Options":"noopen",
   "x-shardid":"96",
   "x-sorting-hat-shopid":"10366451809",
   "Referrer-Policy":"origin-when-cross-origin",
   "x-shopify-api-version":"2020-01",
   "x-shopid":"10366451809",
   "X-XSS-Protection":"1; mode=block; report=/xss-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "X-Content-Type-Options":"nosniff",
   "x-shopify-stage":"production",
   "Vary":"Accept-Encoding",
   "Content-Encoding":"gzip",
   "Content-Security-Policy":"default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopify.cn https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "x-shopify-api-terms":"By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms",
   "Strict-Transport-Security":"max-age=7889238",
   "cf-cache-status":"DYNAMIC",
   "x-permitted-cross-domain-policies":"none",
   "expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
   "x-shopify-shop-api-call-limit":"1/40",
   "x-stats-apipermissionid":"203626283105",
   "Report-To":"{\"group\":\"network-errors\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify\"}]}",
   "Content-Type":"application/json; charset=utf-8",
   "x-stats-userid":"",
   "Transfer-Encoding":"chunked",
   "x-dc":"gcp-us-east1,gcp-us-central1,gcp-us-central1",
   "x-request-id":"86c9a9d7-96cd-4971-b1a6-57112df95a66",
   "Server":"cloudflare",
   "http_x_shopify_shop_api_call_limit":"1/40",
   "cf-ray":"5737dcc7198ff381-ATL",
   "Set-Cookie":"__cfduid=d9afe6e8299af1521516fd6a8cfa91f271584124606; expires=Sun, 12-Apr-20 18:36:46 GMT; path=/; domain=.myshopify.com; HttpOnly; SameSite=Lax",
   "X-Frame-Options":"DENY"
}

首先,非常感谢你

我已经能够通过更新call方法来解决这个问题。显然,当使用HTTP基本身份验证和基于令牌的身份验证时,API不会返回相同的头。通过切换到HTTP Basic Auth,我能够检索目标标头

仅供参考,以下是我现在使用的代码:

  var USERNAME = "myusername";
  var PASSWORD = "mypassword";

    var headers = {
    "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

  var params = {
    "method":"GET",
    "headers":headers
  };

  var finalUrl = 'https://mywebsite/admin/api/2020-01/products.json?limit=250'
  var response = UrlFetchApp.fetch(finalUrl, params);
  var test = response.GetAllHeaders();
  Logger.log(response.getContentText())

我在
test
中也看不到其他一些。像
nel
。如果尝试
Logger.log(JSON.stringify(response.getAllHeaders());Logger.log(JSON.stringify(response.getHeaders())谢谢你的帮助,我刚刚编辑了我的帖子以添加JSON响应。你能看看你的浏览器发送的完整负载并确保发送的是相同的吗?所以我检查了一下,我能看到的唯一区别是,从我的浏览器中,我使用CMS预先设定的HTTP基本身份验证。例如:当我要使用一个带有来自脚本的令牌身份验证的a头时。您是否尝试过在API测试仪中进行此调用?我用。尝试看看是否可以在GAS之外重新创建API调用。一旦你让它在那里工作,用相同的标题在GAS中复制。感谢这是一个我非常薄弱的领域,我感谢你发布你的工作解决方案。你应该问API提供商这是为什么。我觉得基于令牌的身份验证比基本身份验证更安全,因此使用它将是首选/理想选择。谢谢!经过协商,这是API方面的一个临时错误,24小时后该错误已被纠正,我已经能够重新使用基于令牌的身份验证。