使用Fusion tables API和Google Apps脚本时,无法为Content-length设置值

时间:2011-09-21 11:50:08

标签: google-apps-script google-fusion-tables

我有以下Google Apps脚本代码将值POST到Fusion表:

function insertData(authToken){
  var sql="INSERT INTO 1558685(Constituency,Male Voters,Female Voters,Total Population) VALUES ('Langata',65000,5000,350000)";
  query = encodeURIComponent(sql); 
  var URL = "https://www.google.com/fusiontables/api/query/sql=" + query;
  var response = UrlFetchApp.fetch(URL,{method: "post",headers: {"Authorization": "GoogleLogin auth=" + authToken,"Content-Type": "text/html; charset=UTF-8",
                                                                 "Content-length": sql.length,}});

  Logger.log(response.getContentText());

}

每次运行此代码时,都会出现以下错误: 提供无效值的属性:标题:内容长度(第36行)

我已尝试删除'Content-length'标头,但我收到有关 sql参数不能为空的错误

1 个答案:

答案 0 :(得分:1)

请尝试以下方法:

function insertData(authToken){
  var sql="INSERT INTO 1558685 (Constituency,'Male Voters','Female Voters','Total Population') VALUES ('Langata',65000,5000,350000)";
  var query = encodeURIComponent(sql); 
  var URL = "https://www.google.com/fusiontables/api/query";
  var response = UrlFetchApp.fetch(URL, { 
    method: "post",
    headers: { "Authorization": "GoogleLogin auth=" + authToken },
    payload: "sql=" + query // this send the query in the body of the request
  });

 Logger.log(response.getContentText());
}