Javascript 向表字段写入特殊字符时,Fusion表身份验证丢失

Javascript 向表字段写入特殊字符时,Fusion表身份验证丢失,javascript,google-apps-script,google-fusion-tables,Javascript,Google Apps Script,Google Fusion Tables,我对谷歌应用程序脚本和融合表非常陌生。我正在为我的公司开发一个CRM应用程序,我正在使用谷歌网站、谷歌应用程序脚本和fusion table来实现这一点。 我还使用了O'Reilly的《Google脚本-企业应用程序概要》一书中的一个例子来构建我的应用程序。 我使用Fusion table作为我的项目的数据库,我运行doOAuth()函数,允许每个人在Fusion table上执行操作(请参阅下面包含我从书中获得的doOAuth代码的代码)。 该应用程序在我对fusion表发出的每个命令中都能正

我对谷歌应用程序脚本和融合表非常陌生。我正在为我的公司开发一个CRM应用程序,我正在使用谷歌网站、谷歌应用程序脚本和fusion table来实现这一点。 我还使用了O'Reilly的《Google脚本-企业应用程序概要》一书中的一个例子来构建我的应用程序。 我使用Fusion table作为我的项目的数据库,我运行doOAuth()函数,允许每个人在Fusion table上执行操作(请参阅下面包含我从书中获得的doOAuth代码的代码)。 该应用程序在我对fusion表发出的每个命令中都能正常工作。我能够阅读表格的内容,我也能够对其进行搜索,但当涉及到在表格上写入新数据或更新现有数据时,我面临着一个恼人的问题。 我在我的应用程序上有一些文本框,我在其中插入了一些数据,这些数据被写入到表中,到目前为止没有问题,当我向这些文本框添加公共文本时,我在更新数据或向融合表插入数据时没有问题。但当我在文本框中添加任何特殊字符,如:á、ã、á、ä、õ、í等时,我会收到一条消息,告诉我需要授权才能执行该过程。但我已经授权了。似乎授权丢失了。即使我再次运行doOAuth()函数,也不会授予该权限。insertFusionObj()和writeFusionObj()函数(在下面的代码中列出)的参数是特殊字符时似乎有问题。我不知道这是函数的问题还是融合表的插入和更新问题。 如果有人能帮我解决这个问题,我将不胜感激,因为我的应用程序将使用葡萄牙语,其中包含一些特殊字符,当负责向应用程序添加信息的人员使用它时,他可能会在插入和更新融合表时使用特殊字符

我期待着得到一些帮助。 顺致敬意, 拉斐尔·塞蒂·诺盖拉

代码:

/**
*---融合服务---
*
*版权所有(c)2011詹姆斯·费雷拉
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
/**
*融合服务
*@作者詹姆斯·费雷拉
*@文件http://goo.gl/pBSvF
*
*@需要脚本插入ID:?????
*对象服务http://goo.gl/JdEHW
*
*@要求必须在脚本属性中设置融合表ID。
*这将允许您随时使用
*ScriptProperties.setProperty('FUSION_ID','');
*
*
*在融合表中搜索记录
*
*@params{string}目标为所有
*@params{string}其中有效语句请参见http://goo.gl/SkHI1
*“姓氏”包含忽略大小写“Ferr”
*“First Name”包含忽略大小写“J”
*
*@返回{array}[[标题],[匹配行],[匹配行]…]
*如果没有匹配返回[]
*/
功能搜索融合(目标,何处){
var值=[];
如果(目标=='*'){
var headers=getFusionHeaders();
for(标题中的变量i){
push(““+”头[i]+”);
}   
}否则{
价值观。推送(目标);
}
var arrayResult=fusionRequest(“获取”、“选择”+值.toString()+”,ROWID FROM”+
FUSION_ID+“WHERE”+WHERE).split(/\n/);
for(arrayResult中的var i){
arrayResult[i]=arrayResult[i]。拆分(/,/);
}
arrayResult.拼接(arrayResult.length-1,1);
返回arrayResult;
}
/**
*更新融合表中的记录
*注意:fusionObj必须包含Fusion表记录的rowid
*
*@params{object}fusionObj对象,其属性来自cameled列名
*@如果成功,则返回{string}OK
*/
函数writeFusionObj(fusionObj){
var值=[];
var headers=getFusionHeaders();
for(标题中的变量i){
if(fusionObj[camelString(headers[i])!=未定义)
push(“'”+headers[i]+“=”+fusionObj[camelString(headers[i])]+“”);
}  
返回fusionRequest(“post”,“UPDATE”+FUSION_ID+“SET”+values.toString()+
“其中ROWID=”+fusionObj.ROWID+“”)
}
/**
*将新记录添加到融合表
*
*@params{object}fusionObj对象,其属性来自cameled列名
*@returns{integer}rowid对记录的唯一ID有用
*/
函数插入fusionObj(fusionObj){
var值=[];
var列=[];
var headers=getFusionHeaders();
for(标题中的变量i){
push(““+”标题[i]+”);
push(“'”+fusionObj[camelString(headers[i])]+“”);
}  
返回parseInt(fusionRequest(“post”,“插入到”+FUSION\u ID+
(“+columns.toString()+”)值(“+VALUES.toString()+”))。子字符串(5));
}
/**
*获取给定列标题的融合行ID和唯一值
*
*@返回记录的{integer}行ID
*/
函数getFusionROWID(标题、键){
返回p
    /**
     * ---FusionService---
     *
     *  Copyright (c) 2011 James Ferreira
     *
     *  Licensed under the Apache License, Version 2.0 (the "License");
     *  you may not use this file except in compliance with the License.
     *  You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     *  Unless required by applicable law or agreed to in writing, software
     *  distributed under the License is distributed on an "AS IS" BASIS,
     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *  See the License for the specific language governing permissions and
     *  limitations under the License.
     */

    /**
     * FusionService 
     * @author James Ferreira
     * @documentation http://goo.gl/pBSvF
     *
     * @requires Script Insert ID: ?????
     *           ObjService http://goo.gl/JdEHW
     *
     * @requires The Fusion Table ID MUST be set in a Script property. 
     *           This will allow you to change it at anytime using
     *           ScriptProperties.setProperty('FUSION_ID', '<your ID>'); 
     *
     *
     * Search for records in a Fusion Table 
     *
     * @params {string}  target   Quoted column name(s) ('First Name', 'Last Name') OR (*) for all 
     * @params {string}  where    valid statement see http://goo.gl/SkHI1
     *                            'Last Name' CONTAINS IGNORING CASE 'Ferr' 
     *                             AND 'First Name' CONTAINS IGNORING CASE 'J'" 
     *
     * @returns {array}           [[headers],[match row],[match row]...]
     *                            If no match returns []
     */
    function searchFusion(target, where){
      var values = [];
      if (target == '*'){
        var headers = getFusionHeaders();
        for(var i in headers){
          values.push("'"+headers[i]+"'"); 
        }   
      }else{
        values.push(target); 
      }
      var arrayResult = fusionRequest("get", "SELECT "+values.toString()+",ROWID FROM "+
                                      FUSION_ID+" WHERE "+ where).split(/\n/);
      for (var i in arrayResult){
        arrayResult[i] = arrayResult[i].split(/,/); 
      }
      arrayResult.splice(arrayResult.length-1, 1);
      return arrayResult;
    }

    /**
     * Updates a record in the Fusion table
     * Note: fusionObj must contain rowid of Fusion table record
     *
     * @params  {object}  fusionObj  object with properties from cameled column names
     * @returns {string}             OK if successful
     */
    function writeFusionObj(fusionObj){
      var values = [];
      var headers = getFusionHeaders();   
      for(var i in headers){
        if (fusionObj[camelString(headers[i])] != undefined)
        values.push("'"+headers[i] +"'='"+fusionObj[camelString(headers[i])]+"'"); 
      }  
     return fusionRequest("post", "UPDATE "+FUSION_ID+" SET "+values.toString()+
                          " WHERE ROWID = '"+fusionObj.rowid+"'") 
    }

    /**
     * Add a new record to a Fusion table
     *
     * @params  {object}  fusionObj  object with properties from cameled column names
     * @returns {integer}            rowid useful for unique ID of record
     */
    function insertFusionObj(fusionObj){
      var values = [];
      var columns =[];
      var headers = getFusionHeaders(); 

      for(var i in headers){
        columns.push("'"+headers[i] +"'");
        values.push("'"+fusionObj[camelString(headers[i])]+"'"); 
      }  
      return parseInt(fusionRequest("post", "INSERT INTO "+FUSION_ID+
                        " ("+columns.toString()+") VALUES ("+values.toString()+")").substring(5));
    }

    /**
     * Get the Fusion row ID for a given column header and unique value
     *
     * @returns {integer}    Fusion ROWID for record
     */
    function getFusionROWID(header, key){
     return parseInt(fusionRequest("get", "SELECT ROWID FROM "+FUSION_ID+
                                   " WHERE '"+header+"'='"+key+"'").substring(5));
    }

    /**
     * get the column header names from Fusion Table
     *
     * @returns {array}    [header, header, ...]
     */
    function getFusionHeaders(){
      var headers = [];
      var result = fusionRequest("get", "DESCRIBE "+FUSION_ID).split(/\n/);
        for (var i in result){
        result[i] = result[i].split(/,/); 
        headers.push(result[i][1]);  
      }  
      headers.splice(0, 1);
      headers.splice(headers.length-1, 1);
      return headers;  
    }

    /**
     * Deletes a record in the Fusion Table
     *
     * @params  {string}  rowid  the ID of a Fusion table row
     */
    function deleteFusionRow(rowid){  
       fusionRequest("post", "DELETE FROM "+FUSION_ID+" WHERE ROWID = '"+rowid+"'");  
    }

    /**
     * The get or post request to the Fusion API 
     *
     * @params  {string}  reqMethod  get or post
     * @params  {string}  sql        String  A sgl type request see http://goo.gl/aVP3B
     * @returns {integer}            Fusion rowid useful for unique ID of record
     */
    function fusionRequest(method, sql) {

      var url = "https://www.google.com/fusiontables/api/query";

      if (USE_OAUTH){
        var fetchArgs = googleOAuth_();   
      }else{
        var fetchArgs = new Object();
        fetchArgs.headers = {"Authorization": "GoogleLogin auth=" + getAuthToken_()};
      } 
      fetchArgs.method = method; 

      if( method == 'get' ) {
        url += '?sql='+sql;
        fetchArgs.payload = null;
      } else{
        fetchArgs.payload = 'sql='+sql;
      } 
      return UrlFetchApp.fetch(url, fetchArgs).getContentText(); 
    }

    /**
     * @private for OAuth
     */
    function googleOAuth_() {
      var oAuthConfig = UrlFetchApp.addOAuthService('fusion');
      oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
                 "OAuthGetRequestToken?scope=https://www.google.com/fusiontables/api/query");
      oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oAuthConfig.setConsumerKey('anonymous');
      oAuthConfig.setConsumerSecret('anonymous');
      return {oAuthServiceName:'fusion', oAuthUseToken:"always"};
    }


    /**
     * @private for client Auth
     * @returns String(auth token for a user)
     */ 
    function getAuthToken_() {

      var response = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
          method: "post",
          payload: "accountType=GOOGLE" +
                   "&Email=" + ScriptProperties.getProperty('CUSTOMER_KEY') + 
                   "&Passwd=" + encodeURIComponent(ScriptProperties.getProperty('CUSTOMER_SECRET'))+ 
                   "&service=fusiontables" +
                   "&Source=testing"
      });
      var responseStr = response.getContentText();
      responseStr = responseStr.slice(responseStr.search("Auth=") + 5, responseStr.length);
      responseStr = responseStr.replace(/\n/g, "");
      return responseStr;
    }


    /**
     * Used to authenticate to Fusion Tables
     * Run it twice!
     */
    function doOAuth(){
      var method = 'get';
      var sql = "SHOW TABLES";  
      Logger.log(fusionRequest(method,sql));
    }

    var USE_OAUTH = true; 
    var FUSION_ID = ScriptProperties.getProperty('FUSION_ID');