OneStream – execute a rule as an Administrator using REST API


This blog post follows the same idea we covered in my previous post about executing a rule as an Administrator using PowerShell.

We are going to look at how to do this using the REST API in OneStream.

How is it done

You’ll need an external authentication setup for REST API to work. Also, your OS environment must be 5.2+ 🙂

Now that is covered, let’s look at how to use REST API. The documentation for REST API is located under the following URL.

http://<servername>:<portnumber>/OneStreamApi

Like we saw in the previous post, the first thing we need is the SSO token, as that is the only way you can log in using the REST API.

Let’s create a function for that.

GetSSOToken

I’m using Newtonsoft JSON dll for parsing JSON objects. I think this is now included with OneStream (not sure which version it started. I had to add an external reference in 5.3 version, and I see the code is working without that reference in the latest 5.3 patch update.)

I’m using HttpClient. Line 7 is where I’m adding the header to accept JSON.

You’ll need the following to generate the token. My client is using PING for SSO, and yours could be different.

  • Client Secret
  • Client ID
  • Token Manager ID
  • OAuth URL

Line 18 is where you should specify your OAuth URL.

The result is a JSON like the one given below.

All we need to do is parse the JSON and the access_token, and this is what we are doing in lines 21-29.

Public Function GetAccessToken(ByVal si As SessionInfo, ByVal pingBaseURL As String, ByVal clientID As String, ByVal clientSecret As String, ByVal accessTokenMgrID As String) As String
	Try
		Dim accessToken As String = String.Empty
		Dim client As New HttpClient()
		
		' Add headers
		client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

		' Convert the credentials to Base64 endoded string
		Dim authInfo As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(clientID &amp; ":" &amp; clientSecret))
		client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", authInfo)
		
		'create the body Of the request
		Dim tokenBody As String = "grant_type=client_credentials&amp;scope=openid&amp;client_id=" &amp; clientID &amp; "&amp;access_token_manager_id=" &amp; accessTokenMgrID
		' Add content and mention the encoding and the content-type
		Dim content As New StringContent(tokenBody, Encoding.UTF8, "application/x-www-form-urlencoded")
		
		Dim result As HttpResponseMessage = client.PostAsync(pingBaseURL &amp; "/as/token.oauth2", content).Result

		Dim resultContent As String = result.Content.ReadAsStringAsync().Result
		' parse the result using a JObject
		Dim jsonResult As JObject = JObject.Parse(resultContent)
		
		If result.IsSuccessStatusCode Then
			accessToken = jsonResult("access_token")
		Else
			Throw New XFException("Error getting access token" &amp; vbCrLf &amp; result.StatusCode &amp; vbCrLf &amp; result.ReasonPhrase _
			&amp; vbCrLf &amp; jsonResult("error_description").toString() &amp; vbCrLf &amp; jsonResult("error").toString())
		End If	
		
		Return accessToken
		
	Catch ex As Exception
		Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
	End Try
End Function

Running the DM sequence

Now that we got the token, the next step is to run the DM sequence.

We need to add an authorization header to our request, and since our token is of type bearer, we are going to use that as our scheme in Line 7.

Now are going to add a payload to our request. I’m going to use a Dictionary to create a JSON payload. You can use Json conversion to create a formatted payload, as shown below.

Dim dictbody As New Dictionary(Of String, String)
dictbody.Add("BaseWebServerUrl", "your OS URL")
dictbody.Add("ApplicationName", "your app Name")
dictbody.Add("SequenceName", "Your sequence name")
dictbody.Add("CustomSubstVarsAsCommaSeparatedPairs", "var1=var1value,var2=var2value")
Dim payLoad As String = JsonConvert.SerializeObject(dictbody, Formatting.Indented)

It is pretty easy and neat.

Now that we have the payload, all we got to do now is to POST it. This is what is happening at line 20.

Public Sub ExecuteDMSequence(ByVal si As SessionInfo, ByVal OSBaseURL As String, ByVal OSAppName As String, ByVal DMSequenceName As String, ByVal CommaSeperatedParamPairs As String, ByVal XFAPIVersion As String)
	Try
		
		Dim client As New HttpClient()
		
		' add token to the header
		client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer",Me.GetAccessToken(si, "pingBaseURL", "clientID", "clientSecret", "accessTokenMgrID"))
		
		Dim dictbody As New Dictionary(Of String, String)
		dictbody.Add("BaseWebServerUrl", OSBaseURL &amp; "/OneStreamWeb")
		dictbody.Add("ApplicationName", OSAppName)
		dictbody.Add("SequenceName", DMSequenceName)
		dictbody.Add("CustomSubstVarsAsCommaSeparatedPairs", CommaSeperatedParamPairs)
						
		Dim payLoad As String = JsonConvert.SerializeObject(dictbody, Formatting.Indented)
		
		' Add content and mention the encoding and the content-type
		Dim content As New StringContent(payLoad, Encoding.UTF8, "application/json")
		
		Dim result As HttpResponseMessage = client.PostAsync(OSBaseURL + "/OneStreamApi/api/DataManagement/ExecuteSequence?api-version=" + XFAPIVersion, content).Result

		Dim resultContent As String = result.Content.ReadAsStringAsync().Result
		
		If resultContent.XFContainsIgnoreCase("Error") Then
			Throw New XFException(resultContent)
		End If	
		
	Catch ex As Exception
		Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
	End Try
End Sub		

You can call the function as shown below.

Me.ExecuteDMSequence(si, "serverURL", "APP Name", "DM Sequence Name", "InitiatedUserName=" &amp; si.UserName, "5.2.0")

Hope it helps.

Leave a comment

Your email address will not be published. Required fields are marked *