2013-07-27 8 views
6

ho trovato un esempio su come chiamare un webservice con gli script di Google Drive qui: https://developers.google.com/apps-script/articles/soap_geoip_exampleCome convertire una chiamata SoapService deprecato alla nuova UrlFetchApp

function determineCountryFromIP(ipAddress) { 
    var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl"); 
    var geoService = wsdl.getGeoIPService(); 

    var param = Xml.element("GetGeoIP", [ 
       Xml.attribute("xmlns", "http://www.webservicex.net/"), 
       Xml.element("IPAddress", [ 
       ipAddress 
       ]) 
      ]); 

    var result = geoService.GetGeoIP(param); 
    return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text; 
} 

Tuttavia, questo utilizza wich SoapService è deprecato. la documentazione dice che dovrei usare UrlFetchApp Convertire l'input xml è facile. Ma qualcuno può dirmi come chiamare un webservice con UrlFetchApp?

+0

Contrassegna questa domanda con 'google-apps-script' invece di' google-drive -sdk'. –

risposta

8

Si scopre di essere molto più lavoro, ma dopo una giornata di googling e cercando ho preso a lavorare con il UrlFetchApp

function UrlFetchAppDetermineCountryFromIP_(ipAddress) { 
    var xml =   
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
    +"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" 
     +"<SOAP-ENV:Body>" 
     +"<GetGeoIP xmlns=\"http://www.webservicex.net/\">" 
      +"<IPAddress>"+ ipAddress +"</IPAddress>" 
     +"</GetGeoIP>" 
     +"</SOAP-ENV:Body>" 
    +"</SOAP-ENV:Envelope>" 

    var options = 
    { 
    "method" : "post", 
    "contentType" : "text/xml", 
    "payload" : xml 
    }; 

    var result = UrlFetchApp.fetch("http://www.webservicex.net/geoipservice.asmx?wsdl", options); 

    var xmlResult = XmlService.parse(result).getRootElement(); 
    var soapNamespace = xmlResult.getNamespace("soap"); 
    var getGeoIPResponse = xmlResult.getChild("Body", soapNamespace).getChildren()[0]; 
    var getGeoIPResponseNamespace = getGeoIPResponse.getNamespace(); 

    return getGeoIPResponse 
    .getChild("GetGeoIPResult", getGeoIPResponseNamespace) 
    .getChild("CountryCode", getGeoIPResponseNamespace) 
    .getText(); 
} 

Dovrebbe probely essere usa e getta per la costruzione del xml payload con l'XmlService , tuttavia, l'ho provato per alcune ore e non sono riuscito a inserire i 4 attributi xmlns nell'elemento Evnelope, causando il fallimento della richiesta del servizio web

+0

Ricevo una risposta 400 dal server ... dal suo google, non so come sia effettivamente fatta la richiesta, se uso getRequest, la richiesta sembra soddisfacente. Qualsiasi aiuto ? –

+0

Ho appena provato di nuovo oggi e il codice sopra funziona ancora, se si riceve 400 (cattiva richiesta), può essere perché il tuo xml non è correttamente compilato, pensare di sfuggire virgolette doppie e barre rovesciate, anche può essere la decisione del server di destinazione per restituire una richiesta errata anche se la richiesta è corretta ma c'è qualcosa che manca o non piace – Jarno

+0

Grazie per un'ottima risposta! Il primo esempio funzionante di come utilizzare UrlFetchApp per i servizi Web che ho trovato, anche se ho cercato un po 'per farlo! –

Problemi correlati