UrlFetchApp.fetch中缺少响应头

4

我在使用Google AppScript通过API进行工作时遇到了一个问题。

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

我正在使用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 方法返回的标头:

headers that are returned by AppScript method

正如你所看到的,"link"头并不存在。

为了确保问题来自于AppScript,我还从Chrome Inspector中进行了检查: 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"
}

1
我在“test”中也没有看到其他一些内容,例如“nel”。如果您尝试“Logger.log(JSON.stringify(response.getAllHeaders())); Logger.log(JSON.stringify(response.getHeaders()));”,会发生什么? - IMTheNachoMan
谢谢您的帮助,我刚刚编辑我的帖子以添加JSON响应。 - f1rstsurf
你能查看浏览器发送的完整有效载荷,并确保你发送的是相同的吗? - IMTheNachoMan
1
你尝试过在API测试工具中调用吗?我使用https://chrome.google.com/webstore/detail/talend-api-tester-free-ed/aejoelaoggembcahagimdiliamlcdmfm。试着看看是否可以在GAS之外重新创建API调用。一旦你在那里成功了,就用相同的标头在GAS中复制它。 - IMTheNachoMan
谢谢,我会立即尝试并在这里保持联系。 - f1rstsurf
显示剩余3条评论
1个回答

3

首先感谢IMTheNachoMan

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

只是为了提供信息,以下是我现在使用的代码:

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

2
谢谢你发布你的解决方案,这是我非常薄弱的领域,我很感激。 - Cooper
1
你应该询问API提供者为什么会这样。我觉得基于令牌的身份验证比基本身份验证更安全,因此最好/理想的是使用它。 - IMTheNachoMan
谢谢!经过咨询,发现这是API方面的临时错误,24小时后该错误已被纠正,我已能够重新使用基于令牌的身份验证。 - f1rstsurf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接