Clarity

 View Only
  • 1.  GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 02, 2020 10:17 AM
    Edited by Martin Snizek Sep 02, 2020 11:19 AM
    Hi all,
    I have an issue with calling rest API with x-www-form-urlencoded body. It should be used for authorization with Sharepoint, but I am not able to put the body to the request.

    Below you can see my script:

    <gel:script xmlns="http://www.w3.org/2001/XMLSchema" xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" xmlns:sql="jelly:sql" xmlns:xog="http://www.niku.com/xog" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="jelly:util"> <core:catch var="SuperException"> <gel:log>Started 2</gel:log> <core:set var="loginURLstring">https://accounts.accesscontrol.windows.net/myTenantId/tokens/OAuth/2</core:set> <core:new className="java.net.URL" var="loginURL" > <core:arg type="java.lang.String" value="${loginURLstring}" /> </core:new> <gel:log>${loginURL}</gel:log> <core:set var="odeslat_data" value="grant_type=client_credentials&amp;client_id=myClientId&amp;client_secret=myClientSecret=&amp;resource=myResourceValue" /> <gel:log>${odeslat_data}</gel:log> <core:set var="LoginConnection" value="${loginURL.openConnection()}"/> <core:expr value="${LoginConnection.setDoOutput(true)}" /> <core:expr value="${LoginConnection.setDoInput(true)}" /> <core:expr value='${LoginConnection.setRequestMethod("POST")}'/> <core:expr value='${LoginConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded")}'/> <gel:log>${LoginConnection}</gel:log> <gel:log>Connection ready</gel:log> <core:set var="void" value="${LoginConnection.connect()}"/> <gel:log>Connected</gel:log> <core:new className="java.io.OutputStreamWriter" var="wr1"> <core:arg type="java.io.OutputStream" value="${LoginConnection.getOutputStream()}"/> </core:new> <core:set var="void" value="${wr1.write(odeslat_data)}"/> <core:set var="void" value="${wr1.flush()}"/> <core:set var="void" value="${wr1.close()}"/> <gel:log>Body sent</gel:log> <core:set var="loginResponseCode" value="${LoginConnection.getHeaderField(0)}" /> <core:set var="loginResponseMessage" value="${LoginConnection.getResponseMessage()}" /> <gel:log>HTTP response: ${loginResponseCode}</gel:log> <gel:log>Response message: ${loginResponseMessage}</gel:log> </core:catch> <core:if test="${SuperException!=null}"> <gel:log>Super Exception - ${SuperException}</gel:log> </core:if> </gel:script>​

    From postman, my tests finish well but from the script I always get:

    HTTP response: HTTP/1.1 401 Unauthorized

    I suppose that there is a problem with providing the body to the remote webservice, but I have no idea how to do that. Does anyone has any experience or example how to do this?

    Thank you all in advance,
    Martin



  • 2.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 02, 2020 11:22 AM
      |   view attached
    The script as a file, because in my initial message it looks terrible.

    Martin

    Attachment(s)

    xml
    script_shp.xml   2 KB 1 version


  • 3.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 03, 2020 10:45 AM
    Hello Martin, 

    Is this the REST API URL that you are trying to call 

    https://accounts.accesscontrol.windows.net/myTenantId/tokens/OAuth/2


  • 4.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 03, 2020 10:50 AM
    Hello,

    yes, but I have anonymized the tenantId - when I try to call it I replace "myTenantId" with real and in Postman working value.

    Regards,
    Martin


  • 5.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 03, 2020 11:01 AM
    This is how I tried and it is working as expected. 

    <core:set var="apiURL" value="" />
    <core:set var="username" value="" />
    <core:set var="password" value="" />
    <core:new className="java.net.URL" var="remoteURL" >
    <core:arg type="java.lang.String" value="${apiURL}" />
    </core:new>

    <gel:log>${apiURL}</gel:log>

    <core:invokeStatic var="base64" className="com.niku.union.utility.Base64" method="encode">
    <core:arg type="java.lang.String" value="${username}:${password}" />
    </core:invokeStatic>


  • 6.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 03, 2020 11:13 AM
    Hello,
    thank you for your reply, basic authentication works for me as well, but the AuthToken for Sharepoint needs to be obtained by a POST request with body in a x-www-form-urlencoded format. And that is the problem which I am trying to solve - how to post a body in x-www-form-urlencoded format to the API from GEL script.

    Calling of PPM restApi works for me perfectly, but there is a problem with the above described.

    Regards,
    Martin


  • 7.  RE: GEL Script - call rest api with x-www-form-urlencoded body

    Posted Sep 18, 2020 09:29 AM
    Have you looked at the raw output sent of the body sent by postman?

    I would say compare that with what you are sending, as each value in your key-pair query string body should be 'url encoded' if needed.  It's quite possible you're sending escaped & delimiters, or you're not escaping the values (e.g. secrets) that can contain characters that break/terminate them early.