Estimados

Hace unos años usaba un script para poder crear correos corporativos en mi 
ex empresa, el script se alimentaba de columnas que ingresaba una persona. 
y con eso armaba la firma.

Eso fue hace unos años, ahora ha cambiado las cosas en Google ya que me 
salen varios errores.

Error en la solicitud para el código devuelto 401 de 
https://apps-apis.google.com/a/feeds/emailsettings/2.0/globokas.com/[email protected]/signature.
 
Respuesta del servidor truncada: <HTML>
<HEAD>
<TITLE>Token invalid</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
 (usar la opción muteHttpExceptions para examinar toda la respuesta)

Y lo otro es 

Error en la solicitud para el código devuelto 404 de 
https://www.google.com/accounts/ClientLogin. Respuesta del servidor 
truncada:https://developers.google.com/accounts/docs/AuthForInstalledApps
 (usar la opción muteHttpExceptions para examinar toda la respuesta)

les adjunto el codigo para que me digan que puede ser y como es ahora... 


-- 
You received this message because you are subscribed to the Google Groups 
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
var emailAdmin = "[email protected]";
var passAdmin = "passwd123";

// MENU
// ------------------------------------------------- ()
function onOpen() 
{
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var MenuListar = [
    { name : "Actualizar Firmas",
      functionName : "onProcessRecord"},
  ];
  sheet.addMenu("<<Firmas>>", MenuListar);
  
}

function onProcessRecord() {
  try
  {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var hoja = ss.getSheets()[0];    
    
    if (hoja.getLastRow() > 1)
    {
      var idAuth = obtieneIdAutorizacion();
      var plantilla = obtienePlantilla();
      
      var numMaxHoja = hoja.getLastRow() - 1;
      
      //procesa eventos
      for (var ind = 2; ind < numMaxHoja + 2; ind++)
      {
        var datos = hoja.getRange("A" + ind + ":F" + ind).getValues();
        
        var username = datos[0][0];
        var nombre = datos[0][1];
        var cargo = datos[0][2];
        var telefono = datos[0][3];        
        var direccion = datos[0][4];
        var estado = datos[0][5];
        var dominio="globokas.com";
        //grabo firma
        if (estado != "PROCESADO")
        {
          if (grabaFirma(plantilla, idAuth, username,dominio, nombre, cargo, 
telefono,direccion))
          {
            hoja.getRange("F" + ind + ":F" + ind).setValues([["PROCESADO"]]);
          }
        }
      }
    }
  }
  catch (e) 
  {
    MailApp.sendEmail("[email protected]", "Error GAS Firmas v1 
(onProcesando)", e.message);
  }
}
function grabaFirma(plantilla, idAuth, username, dominio, nombre, cargo, 
telefono,direccion)
{
  try
  {
    var headers = 
    {
      "Authorization" : "GoogleLogin auth=" + idAuth
    };

    var aux = quitaCaracteresLatinos(plantilla);
    aux = aux.replace("($NOMBRE$)", quitaCaracteresLatinos(nombre));
    aux = aux.replace("($CARGO$)", quitaCaracteresLatinos(cargo));
    aux = aux.replace("($TELEFONO$)", quitaCaracteresLatinos(telefono));
    aux = aux.replace("($DIRECCION$)", quitaCaracteresLatinos(direccion));

    var xmlFirma = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    xmlFirma += "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\"; 
xmlns:apps=\"http://schemas.google.com/apps/2006\";>";
    xmlFirma += "<apps:property name=\"signature\" value=\"" + 
encodeMyHtml(aux) + "\" />";
    xmlFirma += "</atom:entry>";
    
  
    var options2 =
    {
      "method" : "PUT",
      "contentType" : "application/atom+xml",
      "headers" : headers,
      "payload" : xmlFirma
    };     
    
    
    var respuestaHttp2 = 
UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/"; + 
dominio + "/" + username + "/signature", options2);
    var codigorespuesta = respuestaHttp2.getResponseCode();

    if (respuestaHttp2.getResponseCode() == 200)
    {
      return true;
    }
    else
    {
        return false;
      }
  }
  catch (e) 
  {
    MailApp.sendEmail("[email protected]", "Error GAS Firmas v2 (grabaFirma)", 
e.message);
  }
}
function obtienePlantilla()
{
  var plantillaHTML ="";
  plantillaHTML += "<div style='padding:1em; margin:1em; 
width:100%;font-family:Trebuchet MS, arial; font-size:9pt; color:#333333; 
margin-bottom:100px; '>";
  plantillaHTML += "<div><br /><strong 
style='color:#666666;font-size:10pt'>($NOMBRE$)</strong><br />";
  plantillaHTML += "<strong 
style='color:#666666;font-size:10pt'>($CARGO$)</strong></span><br />";
  plantillaHTML += "<span style='color:#666666'>Tel. (511)-($TELEFONO$)<br />";
  plantillaHTML += "($DIRECCION$)</span></div>";
    //plantillaHTML += "<img style='width:376;height:55' 
src='https://app.miempresa.com/workplace/pics/papel.jpg'>";
  plantillaHTML += "</div>";
  return plantillaHTML; 
 
 
}
function obtieneIdAutorizacion()
{
  try
  {
    var codigoAuth = "";

    var options =
    {
      "method" : "POST",
      "contentType" : "application/x-www-form-urlencoded",
      "payload" : "&Email=" + emailAdmin + "&Passwd=" + passAdmin + 
"&accountType=HOSTED&service=apps"
    };
  
    var respuestaHttp = 
UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin";, options);

    if (respuestaHttp.getResponseCode() == 200)
    {
      codigoAuth = 
respuestaHttp.getContentText().split("Auth=")[1].replace("\n","");
    }
    
    return codigoAuth;
  }
  catch (e) 
  {
    MailApp.sendEmail("[email protected]", "Error GAS Firmas v3 
(obtieneIdAutorizacion)", e.message);
  }
}

function encodeMyHtml(txtHtml) 
{
  var retorno = txtHtml;

  retorno = retorno.replace(/&/g,"&amp;");
  retorno = retorno.replace(/\"/g,"&quot;");
  retorno = retorno.replace(/</g,"&lt;");
  retorno = retorno.replace(/>/g,"&gt;");
  
  return retorno;
}


function quitaCaracteresLatinos(txtHtml) 
{
  var retorno = txtHtml;

  retorno = retorno.replace(/á/g,"&aacute;");
  retorno = retorno.replace(/é/g,"&eacute;");
  retorno = retorno.replace(/í/g,"&iacute;");
  retorno = retorno.replace(/ó/g,"&oacute;");
  retorno = retorno.replace(/ú/g,"&uacute;");

  retorno = retorno.replace(/Á/g,"&Aacute;");
  retorno = retorno.replace(/É/g,"&Eacute;");
  retorno = retorno.replace(/Í/g,"&Iacute;");
  retorno = retorno.replace(/Ó/g,"&Oacute;");
  retorno = retorno.replace(/Ú/g,"&Uacute;");

  retorno = retorno.replace(/ñ/g,"&ntilde;");
  retorno = retorno.replace(/Ñ/g,"&Ntilde;");
  
  return retorno;
}  

function borradodePROCESADO() {
  //remplazar ´firmas´ por el nombre de tu actual hoja de trabajo
  var sheet = SpreadsheetApp.getActive().getSheetByName('firmas');
  sheet.getRange('F2:F257').clearContent();
}

Reply via email to