Reputation: 647
I'm currently facing an issue while working with an API through Google AppScript.
I'm able to query the API and fetch the result, but I need to catch a specific header (link), that contains a key to use in a second query.
I'm using the getAllHeaders() method from HTTPResponse Class, but obviously the targeted header is missing.
Do you have any ideas on how to get this header?
Here is my code :
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));
}
Here are the headers that are returned by AppScript method :
As you can see the "link" header is not present.
To be sure the issue was coming from AppScript I also checked from Chrome Inspector :
JSON Result for getAllHeaders & getHeader method :
{
"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"
}
Upvotes: 4
Views: 1930
Reputation: 647
First of all many thanks to IMTheNachoMan.
I've been able to figure out this issue just by updating the call method. Obviously the API does not return the same headers when using HTTP basic authentication VS token based authentication. By switching to HTTP Basic Auth, i'm able to retrieve the targeted header.
Just for informations, here is the code i now use :
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())
Upvotes: 3