Home > Back-end >  Unable to upload new product to shopify using Google Apps Script
Unable to upload new product to shopify using Google Apps Script

Time:11-03

I have tried a lot but I am still unable to post product to shopify store using Apps Script. I am using the correct key and password but its not working. I am not getting any errors but I get a list of all products in my store in the response.

Here is my code

var storeName = ""
var key = ""
var secret = ""
uRL = "https://" storeName ".myshopify.com/admin/api/2021-10/products.json"
params = {
    "headers": {
    "method":"POST",
    "Authorization": "Basic "   Utilities.base64Encode(key ":" secret),
    "contentType":"application/json",
    "charset":"utf-8"
    },
    "products": {
        "product": {
            "body_html":"",
            "product_type": "JEWELLERY - Bracelets",
            "published_scope": "global",
            "title":"Burton Custom Freestyle 151",
            "vendor":storeName,
            "status":"active",
            "published":true,
            "tags":"",
            "price":"100"
        }
    },
    "muteHttpExceptions" : true
}
var response = UrlFetchApp.fetch(uRL, params);
var r = JSON.parse(response)
console.info(r)

CodePudding user response:

In your situation, how about the following modification?

From:

params = {
    "headers": {
    "method":"POST",
    "Authorization": "Basic "   Utilities.base64Encode(key ":" secret),
    "contentType":"application/json",
    "charset":"utf-8"
    },
    "products": {
        "product": {
            "body_html":"",
            "product_type": "JEWELLERY - Bracelets",
            "published_scope": "global",
            "title":"Burton Custom Freestyle 151",
            "vendor":storeName,
            "status":"active",
            "published":true,
            "tags":"",
            "price":"100"
        }
    },
    "muteHttpExceptions" : true
}

To:

var params = {
  "method": "POST",
  "headers": { "Authorization": "Basic "   Utilities.base64Encode(key   ":"   secret) },
  "contentType": "application/json",
  "muteHttpExceptions": true,
  "payload": JSON.stringify({
    "products": {
      "product": {
        "body_html": "",
        "product_type": "JEWELLERY - Bracelets",
        "published_scope": "global",
        "title": "Burton Custom Freestyle 151",
        "vendor": storeName,
        "status": "active",
        "published": true,
        "tags": "",
        "price": "100"
      }
    }
  })
};

Or,

var params = {
  "method": "POST",
  "headers": { "Authorization": "Basic "   Utilities.base64Encode(key   ":"   secret) },
  // "contentType": "application/json",
  "muteHttpExceptions": true,
  "payload": JSON.stringify({
    "products": {
      "product": {
        "body_html": "",
        "product_type": "JEWELLERY - Bracelets",
        "published_scope": "global",
        "title": "Burton Custom Freestyle 151",
        "vendor": storeName,
        "status": "active",
        "published": true,
        "tags": "",
        "price": "100"
      }
    }
  })
};

References:

  • Related