I have been using Excel VBA to trigger orchestrator jobs using cloud API. Recently, upon checking the API Access window in UiPath portal, there is a note that that functionality will be deprecated in version 21.10.
Has anyone tried the new OAuth feature using Excel as an external application? The documentation I found here is pretty limiting, or maybe I’m just dumb.
This may not be perfect but the below VBA code worked for me:
Public Function GetAccessToken() As String
Dim hReq As Object
Dim strEndPoint As String
Dim strBody As String
Dim strResponse As String
Dim strClientId As String
Dim strClientSecret As String
Dim strScope As String
strEndPoint = "https://cloud.uipath.com/identity_/connect/token"
strClientId = "" ' External Application App ID (Orchestrator)
strClientSecret = "" ' External Application's client secret
strScope = "OR.Jobs" ' External Application's scopes
strBody = _
"grant_type=client_credentials&" & _
"client_id=" & strClientId & _
"client_secret=" & strClientSecret & _
"scope=" & strScope
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "POST", strEndPoint, False
.SetRequestHeader "User-Agent", "Chrome"
.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.Send strBody
strResponse = .ResponseText
End With
Debug.Print strResponse
Dim strToken As String
Dim intStart As Long, intEnd As Long
intStart = InStr(strResponse, """access_token"":""") + 16
intEnd = InStr(strResponse, """,""expires_in""") - 18
strToken = Mid(strResponse, intStart, intEnd)
'Debug.Print strToken
GetAccessToken = strToken
Set hReq = Nothing
End Function
Function TriggerRobot() As Boolean
Dim hReq1 As Object
Dim strEndPoint As String
Dim strTenantName As String
Dim strOrgUnitId As String
Dim strBody As String
Dim strReleaseKey As String
Dim strStrategy As String
Dim strRobotId As String
strEndPoint = YourOrchestratorURL & "/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs"
strTenantName = "" ' Orchestrator Tenant Name
strOrgUnitId = "" ' Folder ID
strReleaseKey = "" ' your process' Job ID/Release Key
strStrategy = "RobotCount"
strRobotId = ""
strBody = _
"{""startInfo"": " & _
"{" & _
"""ReleaseKey"":""" & strReleaseKey & """," & _
"""Strategy"":""" & strStrategy & """," & _
"""RobotIds"":[" & strRobotId & "]," & _
"""NoOfRobots"": 1" & _
"}" & _
"}"
Set hReq1 = CreateObject("MSXML2.XMLHTTP")
With hReq1
.Open "POST", strEndPoint, False
.SetRequestHeader "User-Agent", "Chrome"
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Authorization", "Bearer " & GetAccessToken()
.SetRequestHeader "X-UIPATH-TenantName", strTenantName
.SetRequestHeader "X-UIPATH-OrganizationUnitId", strOrgUnitId
.Send strBody
End With
Set hReq1 = Nothing
End Function