#include "protheus.ch"
#define URL_BASE_SOAP "http://localhost:8080/ws/"
//-------------------------------------------------------------------
/*{Protheus.doc} soapBearer
Consumo de serviço SOAP do Protheus com autenticação bearer
WSINDEX.apw?cOp=02&WSVCNAME=FWAUTHENTICATION
@author Daniel Mendes
@since 07/10/2020
@version 1.0
*/
//-------------------------------------------------------------------
user function soapBearer()
local oWsdl as object
local oXml as object
local cSoapResponse as char
local cError as char
local cWarning as char
oWsdl := TWsdlManager():New()
//Geração do token
oWsdl:ParseURL(URL_BASE_SOAP + "FWAUTHENTICATION.apw?WSDL")
oWsdl:SetOperation("TOKEN")
oWsdl:SetValue(0, "password")
oWsdl:SetValue(3, "admin")
oWsdl:SetValue(4, "1")
oWsdl:SendSoapMsg()
cError := ""
cWarning := ""
cSoapResponse := oWsdl:GetSoapResponse()
oXml := XmlParser(cSoapResponse, "_", @cError, @cWarning )
FreeObj(oWsdl)
oWsdl := nil
//Atualização do token
oWsdl := TWsdlManager():New()
//Verifica o endereço, se existe algum serviço disponível e se existe o serviço que quero utilizar
oWsdl:ParseURL(URL_BASE_SOAP + "FWAUTHENTICATION.apw?WSDL")
oWsdl:SetOperation("TOKEN")
oWsdl:SetValue(0, "refresh_token")
oWsdl:SetValue(5, oXml:_SOAP_ENVELOPE:_SOAP_BODY:_TOKENRESPONSE:_TOKENRESULT:_REFRESH_TOKEN:Text)
oWsdl:SendSoapMsg()
FWFreeVar(@oXml)
cError := ""
cWarning := ""
cSoapResponse := oWsdl:GetSoapResponse()
oXml := XmlParser(cSoapResponse, "_", @cError, @cWarning )
FreeObj(oWsdl)
oWsdl := nil
//Consome um serviço autenticado
oWsdl := TWsdlManager():New()
oWsdl:ParseURL(URL_BASE_SOAP + "FWDICTIONARY.apw?WSDL")
oWsdl:SetOperation("GETTABLELIST")
oWsdl:AddHttpHeader("Authorization", "Bearer " + oXml:_SOAP_ENVELOPE:_SOAP_BODY:_TOKENRESPONSE:_TOKENRESULT:_ACCESS_TOKEN:Text)
oWsdl:SetValue(0, "SED")
oWsdl:SendSoapMsg()
ConOut("GetSoapResponse:", oWsdl:GetSoapResponse())
FreeObj(oWsdl)
oWsdl := nil
return |