google-apps-script - UrlFetchApp.fetch 中缺少响应 header

标签 google-apps-script http-headers shopify urlfetch

我目前在通过 Google AppScript 使用 API 时遇到问题。

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

我正在使用来自 HTTPResponse Class 的 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 方法返回的 header :

headers that are returned by AppScript method

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

为确保问题出自 AppScript,我还从 Chrome Inspector 进行了检查:
headers returned by Chrome Inspector

getAllHeaders 和 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"
}

最佳答案

首先非常感谢IMTheNachoMan .

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

仅供引用,这是我现在使用的代码:

  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())

关于google-apps-script - UrlFetchApp.fetch 中缺少响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675270/

相关文章:

google-api - 谷歌脚本 API : How to apply an onEdit function to every spreadsheet?

url - 如何在 webapp 中获取 google script webapp 的 URL

google-app-engine - 从 GAE 请求时从 make_fetch_call 中删除 header (用户代理)

java - 如何从包含文本 header 的套接字输入流中读取二进制文件? :

http-headers - X-P2P-PeerDist header : where does it come from?

html - 高度 :100% does not work to resize my div

javascript - 如何防止 Shopify Polaris React 应用过早连接到 Shopify 服务?

Shopify:是否可以通过 sku 编号查找产品?

google-apps-script - Google Chat bot - 发送没有事件的私有(private)消息

javascript - 如何将变量传递到 HTML 正文中