Calling One IAM REST API from VB.NET

Hi All,

I need some help from you.

I have below the PowerShell code and it is working fine.

We have a requirement to call this One IAM rest API from VB.Net  code and don't know how to pass all these parameters. Looked on google and tried but always failed.

Does anyone has called REST API from VB.NET code and if so could you please share sample code. Thank you in advance.

$authdata = @{AuthString="Module=DialogUser;User=***;Password=*****"}

$authJson = ConvertTo-Json $authdata -Depth 2

# Login (important, pass the NAME for your session variable in -SessionVariable)

Invoke-RestMethod -Uri "http://<Server>/AppServer/auth/apphost" -Body $authJson.ToString() -Method Post -UseDefaultCredentials -Headers @{Accept="application/json"} -SessionVariable wsession

# Sample 1: Load collection using Post method

$body = @{parameters = @("test1284@test.com","98456798","FirstName","LastName","Middle Name","Department name")} | ConvertTo-Json

Invoke-RestMethod -Uri "http://<Server>//AppServer/api/script/CCC_CreateDepartment_TPA" -WebSession $wsession -Method PUT -Body $body -ContentType application/json

Invoke-RestMethod -Uri "http://<Server>//AppServer/auth/logout" -WebSession $wsession -Method Post
Kind Regards,
Dnyandev
  • I do not have the full sample code for your question, but the following sample code I have found should answer most of the questions of how to do this in general.

    It uses the 3rd party components RestSharp and NewtonSoft.Json.


    Dim client As New RestSharp.RestClient("http://myAppServer/AppServer")
    client.CookieContainer = New System.Net.CookieContainer
    
    ' Authenticate against OneIM AppServer
    Dim request = New RestSharp.RestRequest("auth/apphost", RestSharp.Method.POST)
    Dim Body As String = "{""authString"":""Module=DialogUser;User=viadmin;Password=myPassword""}"
    request.RequestFormat = RestSharp.DataFormat.Json
    request.AddHeader("Content-Type", "application/json")
    request.UseDefaultCredentials = True
    request.AddParameter("application/json", Body, RestSharp.ParameterType.RequestBody)
    
    Dim response As RestSharp.IRestResponse = client.Execute(request)
    If response.StatusCode = Net.HttpStatusCode.OK Then
    	' Fetch password
    	request.Parameters.Clear()
    	request.Resource = "api/entities/DialogConfigParm"
    	request.Method = RestSharp.Method.GET
    	request.AddQueryParameter("Fullpath", "Custom\DynamicSyncPW")
    	request.AddQueryParameter("displayColumns", "Value")
    	response = client.Execute(request)
    	Dim responseObj As Newtonsoft.Json.Linq.JArray = Newtonsoft.Json.Linq.JArray.Parse(response.Content)
    	Dim pw As String = responseObj.SelectToken("[0].values.Value").ToStringNullSafe
    	If Not String.IsNullOrEmpty(pw) Then
    		log.Debug("Dynamic Sync Password:{0}",pw)
    		Return pw
    	Else
    		Throw New Exception("Password could not be determined.")
    	End If
    End If
    
    Throw New Exception("Password could not be determined.")

    HTH

  • Thank you Markus for your help.

    This helped me a lot.

  • Hi Markus

    I’m facing the same problem, I think.

    I have created a script API called XXXX (just for test) as we need to implement some custom APIs.

    I published it and from https://<hostname>/AppServer/swagger-ui/#/ I can easily consume my API

    In order to call it, I provide the 3 parameters my script needs, so I put the following

    {

      "parameters": [

                   "RequestId",

                   "RequestStatus",

                   "Description"

      ]

    }

    Into the field and I press the button “Execute”. As I told you, it works like a charm.

     

    The problems raise when we try to call the API from a script. We are following all the instructions and the examples but:

    1. If we try to call it from Powershell, there is no way we can pass the parameters à error message: Invoke-RestMethod : {"responseStatus":{"errorCode":"ArgumentException","message":"Method expected 3 parameters but got 0.","errors":[]}}

    So my question is: how do I send the parameters?

    1. If we try from vb.net script, there is no way we can authenticate. I mean, I can call the “auth/apphost” and it seems everything is ok, but then, what do I need to do to call my XXXX API? I think the script you added is very good, but there is no way I can install RestSharp on production. So I’m asking if you have an example with no 3rd party components.

    Thanks in advance and sorry for bother you, but I’m really stuck on this.

    Alberto

     

  • The PowerShell code to call the script would look like the following:

    $authdata = @{AuthString="Module=DialogUser;User=<user name>;Password="}
    $authJson = ConvertTo-Json $authdata -Depth 2
    
    # Login (important, pass the NAME for your session variable in -SessionVariable)
    Invoke-RestMethod -Uri "https://<Hostname>/AppServer/auth/apphost" -Body $authJson.ToString() -Method Post -UseDefaultCredentials -Headers @{Accept="application/json"} -SessionVariable wsession
    
    # Script Call
    $body=@{parameters=@("RequestId","RequestStatus","Description")} | ConvertTo-Json
    (Invoke-RestMethod -Uri "https://<Hostname>/AppServer/api/script/<scriptname>" -WebSession $wsession -Method Put -Body $body -ContentType application/json).result
    

    And because the code is a little bit complex if you want to call a RestAPI without helper modules like RestSharp, I am unable to help you with pure VB.Net code.

    But the hint is, that you must save and pass the session object you have created during the auth-call to any subsequent calls. (this is the WSession thing in the PowerShell sample).

  • Markus,

    you did it again :-) !!

    Thanks a lot. Our mistake was:

     

    $body=@{parameters="[RequestId","RequestStatus","Description]"} | ConvertTo-Json

     

    Instead of

     

    $body=@{parameters=@("RequestId","RequestStatus","Description")} | ConvertTo-Json

     

     

    Thank you, sincerely!

     

    About the vb.net, yes, it is pretty cleat to me what you stated: “But the hint is, that you must save and pass the session object you have created during the auth-call to any subsequent calls. (this is the WSession thing in the PowerShell sample).” but thank you for your confirmation.  

     

    Alberto