Page 1 of 1

Autentication issue with cUrl

Posted: Fri Oct 28, 2022 8:40 am
by Marc Venken
In some sites I can retrieve webdata with this :

cLink = "https://site/api/v2/products/"+alltrim(cCode)+"?search=sku&token=token"
MsgRun( cLink, "READING FORUM PAGE", { || uResponse := WebPageContents( cLink, .t. ) } )

Now the new site is giving a :

"message":"Unauthorized"

I tried a sample from Tim : (changed real token to text token just for forum )
I don't know with data is needed in cData ? Tim ??

If I use the API on there test site, it works, and I get this information :

curl -X GET "https://site/v3/products/inventory?catalog_reference=B10" -H "accept: application/json" -H "x-site-authorization: token" -H "x-api-key: apikey here"

Tim's code (Thanks)

Code: Select all | Expand

  local cUrl := "https://siteo/v3/products/inventory?catalog_reference=Gi6400B?token=token"
  local cData := ""
  local cContentType := "application/json"
  local cAuthorization := cToken //  cToken is only the token and not "x-site-authorization: token" ???  is this the issue ?

  PostTo360( cUrl, cData, cContentType,cAuthorization )

Function PostTo360( cUrl, cData, cContentType,cAuthorization )

    Local oOle,cRet:='',uRet
    default cContentType:="application/json"
    default cAuthorization:=""
    oOle   := CreateObject( "MSXML2.XMLHTTP" )
    oOle:Open( 'GET', cUrl, .f. )
    oOle:SetRequestHeader( "Content-Type", cContentType)
    oOle:SetRequestHeader( "Authorization",cAuthorization)
    oOle:Send( cData )
    cRet := oOle:ResponseBody
    msginfo(cRet)
RETURN( cRet )
 

Re: Autentication issue with cUrl

Posted: Fri Oct 28, 2022 9:33 am
by Silvio.Falconi
I'm also interested in having a working test sample

Re: Autentication issue with cUrl

Posted: Fri Oct 28, 2022 10:16 am
by Marc Venken
Houston... I get some data ... :D :D

With this updated code, I get some more data, but not the good once.

Code: Select all | Expand


Function PostTo360( cUrl, cData, cContentType,cAuthorization )

    Local oOle,cRet:='',uRet

    default cContentType:="application/json"
    default cAuthorization:=""

    oOle   := CreateObject( "MSXML2.XMLHTTP" )
    oOle:Open( 'GET', cUrl, .f. )
    oOle:SetRequestHeader("content-type", "application/json" )
    oOle:SetRequestHeader( "Authorization","Bearer"+cAuthorization)

    TRY
        oOle:Send( cData )
        cRet := oOle:ResponseBody

    CATCH

        //NOS GUSTARIA PODER CAPTURAR EL ERROR Y SABER EL MOTIVO POR EL QUE NO SE PUDO ENVIAR
        msginfo("Fout - Error URL","error url")
        ? "Return : "+cRet
    END
    FW_memoEdit(cRet)

RETURN( cRet )

 
The response :

{"message":"Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=token"}

Re: Autentication issue with cUrl

Posted: Fri Oct 28, 2022 10:18 am
by Marc Venken
Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=allow

indicates that the resource path doesn't exist. Check your url and make sure you're accessing the correct endpoint.


Sorry, I have to look further...