尝试了许多选项来使用 Google Apps Script UrlFetchApp.fetch() put 方法更新 ECWID 中的产品,但没有成功。以下是我编写代码和测试的不同方式,但会遇到不同类型的错误。
我想,我错过了一些小东西,无法弄清楚。请帮我解决这个问题。
API:ECWID Products API ( http://kb.ecwid.com/w/page/25285101/Product%20API#RESTAPIMethodupdateaproduct ) 方法:PUT(更新产品详情)
示例代码 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": "OTHER", "errorMessage": "解析 JSON 时出错:JSONObject 文本必须在字符 0 处以 '{' 开头" }"
版本 2:- 尝试将对象转换为 json stringify,但同样的错误。
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": "OTHER", "errorMessage": "解析 JSON 时出错:JSONObject 文本必须在字符 0 处以 '{' 开头" }"
版本 3:- (尝试在标头中使用 Authorization 传递 secure_auth_key)
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": "OTHER", "errorMessage": "API key not found in request parameters" }
另请注意,我尝试使用 DevHttpClient chrome 插件,它正在正确更新。这意味着我们使用 UrlFetch 的方式存在一些问题。请帮我解决这个问题...
提前致谢...