简体   繁体   中英

UrlFetch put method using Google Apps Script

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM