0
votes

Have tried many options to update a product in ECWID using Google Apps Script UrlFetchApp.fetch() put method but not succeeded. Following are the different ways that I've written the code and tested, but am getting different type of errors.

I guess, am missing some small thing, which am not able to figure it out. Please help me to fix this issue.

API: ECWID Products API (http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodupdateaproduct) Method: PUT (to update the product details)


Sample Code 1:-

function updateProducts(){
var products_authkey = "xxxxxxxx";

  try{
    var url ="https://app.ecwid.com/api/v1/xxxxx/product?id=xxxxxxxx&secure_auth_key="+products_authkey;
    var payload = {price:62755};

    var options ={method:"put",ContentType:"application/json",payload:payload};    
    var result = UrlFetchApp.fetch(url, options);    
    var response = result.getContentText();
  }catch(e){
    Browser.msgBox(e);
  }
}

Error:- "{ "error": "OTHER", "errorMessage": "Error parsing JSON: A JSONObject text must begin with '{' at character 0" }"


Version 2:- Tried converting the object to json stringify, but the same error.

function updateProducts_version2(){
  try{
    var url ="https://app.ecwid.com/api/v1/xxxx/product?id=xxxxx&secure_auth_key="+products_authkey;
    var payload = {price:62755};
    var payload_json = Utilities.jsonStringify(payload);
    var options ={method:"put",ContentType:"application/json",payload:payload_json,muteHttpExceptions:true};
    var result = UrlFetchApp.fetch(url, options);
    var response = result.getContentText();
    var res_code = result.getResponseCode();
    var x = 1;
  }catch(e){
    Browser.msgBox(e);
  }
}

Error:- "{ "error": "OTHER", "errorMessage": "Error parsing JSON: A JSONObject text must begin with '{' at character 0" }"


Version 3:- (Tried passing secure_auth_key using Authorization in headers)

function updateProducts_version3(){
  try{
    var url ="https://app.ecwid.com/api/v1/xxxxx/product?id=xxxxx";
    var payload = {price:62755};
    var headers = {Authorization: 'xxxxxxx'};
    var options = {headers:headers,method:"put",ContentType:"application/json",payload:payload};
    var options ={method:"put",ContentType:"application/json",payload:payload,muteHttpExceptions:true};
    var result = UrlFetchApp.fetch(url, options);
    var response = result.getContentText();
    var res_code = result.getResponseCode();
    var x = 1;
  }catch(e){
    Browser.msgBox(e);
  }
}

Error:- { "error": "OTHER", "errorMessage": "API key not found in request parameters" }


Also to note that, I've tried using DevHttpClient chrome plugin, it's updating properly. Which means that there's some problem the way we're using UrlFetch. Please help me in fixing this issue...

Thanks in advance...

1
would it be simply that you need to put your payload in quotes?patt0
Have you tried using the Debug option (you might have to remove your try)? Then you'll be able to see exactly what the payload looks like. I'm curious as to which line is throwing the error. I don't have creds for the service, but I'm getting an expected 400 Error on the UrlFetchApp.fetch line.fooby
@patto: have tried by having the object in quotes like the below, then also getting the same error as before: var payload = "{price:62758}";pavan
@fobby: Below is the content in debug mode: payload Object (207294512) ({price:62758}) price number 62758.. And it's failing at URLFetchApp.fetch with the errors mentioned abovepavan
Why two "options" in v4? The second one left out the headers...Mogsdad

1 Answers

1
votes

Credentials are needed to test this, so that's up to you. You probably need to both stringify & encode the payload. You also had incorrect capitalization on contentType, which you could check with UrlFetchApp.getRequest().

function updateProducts_version2a(){
  try{
    var url ="https://app.ecwid.com/api/v1/xxxx/product?id=xxxxx&secure_auth_key="+products_authkey;
    var payload = {price:62755};
    var payload_json = encodeURIComponent(JSON.stringify(payload));
    var options ={method:"put",contentType:"application/json",payload:payload_json,muteHttpExceptions:true};
    var result = UrlFetchApp.fetch(url, options);
    var response = result.getContentText();
    var res_code = result.getResponseCode();
    var x = 1;
  }catch(e){
    Browser.msgBox(e);
  }
}

This next version seemed to work - by suppressing the price change and using a store's ID, it mimicked a product 'get', according to the docs you referenced. This time, the error message might be indicating some level of success: "This Ecwid account doesn't have access to Ecwid API. Please, consider upgrading it."

You'll notice that the URL has been separated out, with the basic header info of product ID and auth key together.

function updateProducts_version4(){
  try{
    var url ="https://app.ecwid.com/api/v1/xxxx/product";
    var payload = encodeURIComponent(JSON.stringify({
        price:62755
      }));
    var headers = {id:'xxxx',
                   secure_auth_key: 'xxxxxxx'
                  };
    var options = {
      headers:headers,
      method:"put",
      contentType:"application/json",
      muteHttpExceptions:true,
      payload:payload
    };
    var request = UrlFetchApp.getRequest(url, options);  // Debug: check what would be fetched
    var result = UrlFetchApp.fetch(url, options);
    var response = result.getContentText();
    var res_code = result.getResponseCode();
    var respHeaders = result.getHeaders(); ///
    debugger;
  }catch(e){
    Logger.log(e);
    //Browser.msgBox(e);
  }
}

Without your creds, that's as far as I can take it... tell us how that works for you.