如何从谷歌应用程序脚本设置会话cookie

时间:2017-02-26 01:38:26

标签: authentication cookies soap google-apps-script session-cookies

从谷歌应用程序脚本我使用此代码向SOAP API服务发送请求。

它返回一个验证字符串,可用于设置设置的会话cookie。我只是不知道该怎么做。

有人可以帮忙吗?

祝你好运 Kresten

function e_conomic_ConnectWithToken(token, appToken) {

  var e_conomic_WSDL = "https://api.e-conomic.com/secure/api1/EconomicWebService.asmx?WSDL";

  var envelop_ConnectWithToken = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                 "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" + 
                                 "<x:Header/><x:Body>" + 
                                 "<eco:ConnectWithToken>" +
                                 "<eco:token>" + token + "</eco:token>" +
                                 "<eco:appToken>" + appToken + "</eco:appToken>" +
                                 "</eco:ConnectWithToken>" +
                                 "</x:Body></x:Envelope>";

  var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_ConnectWithToken};
  var response = UrlFetchApp.fetch(e_conomic_WSDL, options);

  // parse the soap response
  var response_XML = XmlService.parse(response).getRootElement();
  var soap_Namespace = response_XML.getNamespace("soap");
  var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

  var firstChildOfBodyName = firstChildOfBody.getName();

  // if it is a soap fault, throw error with faultstring
  if(firstChildOfBodyName === "Fault") {
    var reason = firstChildOfBody.getChildren()[1].getText();
    throw new Error(reason);
  } 

  // otherwise extract the data from soap response
  else {
    return firstChildOfBody.getChildren()[0].getText();
  }
}

1 个答案:

答案 0 :(得分:0)

这是我最终得到的代码。 如果它可以帮助您,请随时使用它。

这是很久以前的事情了,所以我不能为您提供更多帮助,而且我也无法再访问该端点,因此也无法对其进行测试。

BR Kresten

function e_conomic_SOAPClient() {

    var wsdlUrl = "https://api.e-conomic.com/secure/api1/EconomicWebService.asmx?WSDL";
    
    var sessionCookie = "";

    var self = this;

    self.GetWSDLURL = function() {
        return wsdlUrl;
    }

    self.GetSessionCookie = function() {
        return sessionCookie;
    }

    
    // implement requests for connection here
    
    
    self.ConnectWithToken = function(token, appToken) {

        var envelop_ConnectWithToken = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                       "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" + 
                                       "<x:Header/><x:Body>" + 
                                       "<eco:ConnectWithToken>" +
                                       "<eco:token>" + token + "</eco:token>" +
                                       "<eco:appToken>" + appToken + "</eco:appToken>" +
                                       "</eco:ConnectWithToken>" +
                                       "</x:Body></x:Envelope>";

        var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_ConnectWithToken};
        var response = UrlFetchApp.fetch(self.GetWSDLURL(), options);

        // parse the soap response
        var response_XML = XmlService.parse(response).getRootElement();
        var soap_Namespace = response_XML.getNamespace("soap");
        var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

        var firstChildOfBodyName = firstChildOfBody.getName();

        // if it is a soap fault, throw error with faultstring
        if(firstChildOfBodyName === "Fault") {
            var reason = firstChildOfBody.getChildren()[1].getText();
            throw new Error(reason);
        } 

        // otherwise return current object with sessionCookie
        else {
            sessionCookie = response.getAllHeaders()['Set-Cookie'].join(";"); 
            return self;
        }
    }
    
    
    
    
    self.Disconnect = function() {

        var envelop_Disconnect = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                 "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" + 
                                 "<x:Header/><x:Body>" + 
                                 "<Disconnect xmlns=\"http://e-conomic.com\" />" +
                                 "</x:Body></x:Envelope>";

        var header = {"Cookie" : self.GetSessionCookie()};
        var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_Disconnect, "headers" : header};
        var response = UrlFetchApp.fetch(self.GetWSDLURL(), options);

        // parse the soap response
        var response_XML = XmlService.parse(response).getRootElement();
        var soap_Namespace = response_XML.getNamespace("soap");
        var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

        var firstChildOfBodyName = firstChildOfBody.getName();

        // if it is a soap fault, throw error with faultstring
        if(firstChildOfBodyName === "Fault") {
            var reason = firstChildOfBody.getChildren()[1].getText();
            throw new Error(reason);
        } 

        // otherwise remove sessionCookie
        else {
            sessionCookie = "";
        }
    }
}



// implement authenticated requests here


// *********************************************************************************************************************************************************


e_conomic_SOAPClient.prototype.Account_GetAll = function() {

    var envelop_Account_GetAll = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                 "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" +
                                 "<x:Header/><x:Body>" +
                                 "<eco:Account_GetAll/>" +
                                 "</x:Body></x:Envelope>";
  
    var header = {"Cookie" : this.GetSessionCookie()};
    var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_Account_GetAll, "headers" : header};
    var response = UrlFetchApp.fetch(this.GetWSDLURL(), options);

    // parse the soap response
    var response_XML = XmlService.parse(response).getRootElement();
    var soap_Namespace = response_XML.getNamespace("soap");
    var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

    var firstChildOfBodyName = firstChildOfBody.getName();

    // if it is a soap fault, throw error with faultstring
    if(firstChildOfBodyName === "Fault") {
        var reason = firstChildOfBody.getChildren()[1].getText();
        throw new Error(reason);
    } 

    // otherwise extract the data from soap response
    else {
        var account_all_array = firstChildOfBody.getChildren()[0].getChildren();
        var array = [];
        for(var c = 0; c < account_all_array.length; c++) {
            array.push(account_all_array[c].getChildren()[0].getText());
        }
        return array;
    }
}


// *********************************************************************************************************************************************************


e_conomic_SOAPClient.prototype.Debtor_GetAll = function() {

    var envelop_Debtor_GetAll = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                 "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" +
                                 "<x:Header/><x:Body>" +
                                 "<eco:Debtor_GetAll/>" +
                                 "</x:Body></x:Envelope>";
  
    var header = {"Cookie" : this.GetSessionCookie()};
    var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_Debtor_GetAll, "headers" : header};
    var response = UrlFetchApp.fetch(this.GetWSDLURL(), options);

    // parse the soap response
    var response_XML = XmlService.parse(response).getRootElement();
    var soap_Namespace = response_XML.getNamespace("soap");
    var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

    var firstChildOfBodyName = firstChildOfBody.getName();

    // if it is a soap fault, throw error with faultstring
    if(firstChildOfBodyName === "Fault") {
        var reason = firstChildOfBody.getChildren()[1].getText();
        throw new Error(reason);
    } 

    // otherwise extract the data from soap response
    else {
        var debator_all_array = firstChildOfBody.getChildren()[0].getChildren();
        var array = [];
        for(var c = 0; c < debator_all_array.length; c++) {
            array.push(debator_all_array[c].getChildren()[0].getText());
        }
        return array;
    }
}


// *********************************************************************************************************************************************************


e_conomic_SOAPClient.prototype.newEntry = function(konto, dato, nr, tekst, beloeb, momskode) {

if (momskode==="I25"){  var envelop_newEntry = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" +
    "<x:Header/>"+
    "<x:Body>"+

        "<eco:CashBookEntry_CreateFromData>"+
            "<eco:data>"+
                "<eco:Type>FinanceVoucher</eco:Type>"+
                "<eco:CashBookHandle><eco:Number>1</eco:Number></eco:CashBookHandle>"+
                "<eco:AccountHandle><eco:Number>" + konto + "</eco:Number></eco:AccountHandle>"+
                //"<eco:ContraAccountHandle><eco:Number>" + modkonto + "</eco:Number></eco:ContraAccountHandle>"+
                "<eco:Date>" + dato + "T00:00:00</eco:Date>"+
                "<eco:VoucherNumber>" + nr + "</eco:VoucherNumber>"+
                "<eco:Text>"+ tekst + "</eco:Text>"+
                "<eco:AmountDefaultCurrency>0</eco:AmountDefaultCurrency>"+
                "<eco:CurrencyHandle><eco:Code>DKK</eco:Code></eco:CurrencyHandle>"+
                "<eco:Amount>" + -beloeb + "</eco:Amount>"+
                "<eco:VatAccountHandle><eco:VatCode>" + momskode + "</eco:VatCode></eco:VatAccountHandle>"+
            "</eco:data> "+
        "</eco:CashBookEntry_CreateFromData>"+

    "</x:Body>" +
"</x:Envelope>";}

else {var envelop_newEntry = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" +
    "<x:Header/>"+
    "<x:Body>"+

        "<eco:CashBookEntry_CreateFromData>"+
            "<eco:data>"+
                "<eco:Type>FinanceVoucher</eco:Type>"+
                "<eco:CashBookHandle><eco:Number>1</eco:Number></eco:CashBookHandle>"+
                "<eco:AccountHandle><eco:Number>" + konto + "</eco:Number></eco:AccountHandle>"+
                //"<eco:ContraAccountHandle><eco:Number>" + modkonto + "</eco:Number></eco:ContraAccountHandle>"+
                "<eco:Date>" + dato + "T00:00:00</eco:Date>"+
                "<eco:VoucherNumber>" + nr + "</eco:VoucherNumber>"+
                "<eco:Text>"+ tekst + "</eco:Text>"+
                "<eco:AmountDefaultCurrency>0</eco:AmountDefaultCurrency>"+
                "<eco:CurrencyHandle><eco:Code>DKK</eco:Code></eco:CurrencyHandle>"+
                "<eco:Amount>" + beloeb + "</eco:Amount>"+
//                "<eco:VatAccountHandle><eco:VatCode>" + momskode + "</eco:VatCode></eco:VatAccountHandle>"+
            "</eco:data> "+
        "</eco:CashBookEntry_CreateFromData>"+

    "</x:Body>" +
"</x:Envelope>";}
  
    var header = {"Cookie" : this.GetSessionCookie()};
    var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_newEntry, "headers" : header};
    var response = UrlFetchApp.fetch(this.GetWSDLURL(), options);
  
}



// *********************************************************************************************************************************************************

//get number

e_conomic_SOAPClient.prototype.getNextVoucherNumber = function() {

    var envelop_GetNextVoucherNumber = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                       "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eco=\"http://e-conomic.com\">" +
                                       "<x:Header/><x:Body>" +
                                       "<eco:CashBook_GetNextVoucherNumber><eco:cashBookHandle><eco:Number>1</eco:Number></eco:cashBookHandle></eco:CashBook_GetNextVoucherNumber>" +                               
                                       "</x:Body></x:Envelope>";
  
    var header = {"Cookie" : this.GetSessionCookie()};
    var options = {"method" : "post", "contentType" : "text/xml", "muteHttpExceptions" : true, "payload" : envelop_GetNextVoucherNumber, "headers" : header};
    var response = UrlFetchApp.fetch(this.GetWSDLURL(), options);

    // parse the soap response
    var response_XML = XmlService.parse(response).getRootElement();
    var soap_Namespace = response_XML.getNamespace("soap");
    var firstChildOfBody = response_XML.getChild("Body", soap_Namespace).getChildren()[0];

    var firstChildOfBodyName = firstChildOfBody.getName();

    // if it is a soap fault, throw error with faultstring
    if(firstChildOfBodyName === "Fault") {
        var reason = firstChildOfBody.getChildren()[1].getText();
        throw new Error(reason);
    } 

    // otherwise extract the data from soap response
  else {
   
    return firstChildOfBody.getChildren()[0].getText();
  }
}