Jump to content
Welcome to our new Citrix community!
  • 1

Trying to send a message to user using REST API


Ashlesh Shenoy

Question

Good Morning All,

 

I am working towards a script that would send a message/notification in their session.

But, the script is erroring out. Can anyone help me understand what I am doing wrong?

 

Script (PowerShell):

$tokenUrl = 'https://api-eu.cloud.com/cctrustoauth2/root/tokens/clients'

$response = Invoke-WebRequest $tokenUrl -Method POST -Body @{
  grant_type = "client_credentials"
  client_id = $env:CITRIX_CLIENT_ID
  client_secret = $env:CITRIX_CLIENT_SECRET
}

$token = $response.Content | ConvertFrom-Json

$headers = @{
  Authorization = "CwsAuth Bearer=$($token.access_token)";
  'Citrix-CustomerId' = $env:CITRIX_CLIENT_CUSTOMER_ID;
  Accept = 'application/json';
}

$site_id = $env:CITRIX_SITE_ID

$requestUri = "https://api-eu.cloud.com/cvadapis/"+$site_id+"/Sessions"

$response = Invoke-WebRequest -Uri $requestUri -Method GET -Headers $headers
$response = $response | ConvertFrom-Json

foreach ($session in $response.Items)
{    
    if($session.User.PrincipalName -eq "username@domain.com")
    {
        $session_id = $session.Id
        

        $body = @{
          "Style" = "Information";
          "Title" = "Test - Title";
          "Text" = "Test - Body";
        }
      
        $requestUri = "https://api-eu.cloud.com/cvadapis/$site_id/Sessions/$session_id/`$sendMessage"

        $response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body $body -ContentType "application/json"
        $response1.status_code
    }    

}

Error Message:

{   "ErrorMessage": "Invalid arguments specified.\r\nParameter name: id",   "SdkErrorId": "UnknownError",   "ErrorType": "ArgumentException",   "ErrorDetails":
     | "Transaction ID: 60340e6a-90c2-412d-bc1f-66297e40fca4\r\nAction Name: SendSessionMessage\r\n\r\nAn error occurred. Contact Citrix Technical Support, and
     | provide the transaction ID above.\r\n\r\nException:\r\n    Error Source : CitrixOrchestration\r\n    \r\n",   "SupportLink": null,   "ErrorReportXml": null,
     | "ErrorReport": null }

Link to comment

4 answers to this question

Recommended Posts

  • 1
3 minutes ago, Pat Patterson said:

It looks like you forgot to JSON-encode the payload to the POST. Try changing this line:

 


$response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body $body -ContentType "application/json"

 

to

 


$response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body ($body | ConvertTo-Json)  -ContentType "application/json"

 

Wow, I cannot believe that is what I was messing up.
It worked and I appreciate your help and prompt response.

  • Like 2
Link to comment
  • 0
3 minutes ago, Pat Patterson said:

Hi! Could you add some `Write-Output` calls to your script to help narrow down which call is failing? It's hard to see whether that output is from the authentication, the request for the sessions, or the message send.

Pat, I apologize, I should have included what line is causing the script to error out.

The requestUri is https://api-eu.cloud.com/cvadapis/1bd0641e-fdb8-49fd-ae04-8d14ab5ae527/Sessions/3817dd50-c6db-440b-b4f6-9c8a39139347/$sendMessage

I have no issues in the script until line 39 ($response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body $body -ContentType "application/json")

This is the error message I get,
image.thumb.png.e7b7b5be7160502156ff6f4908343480.png

Link to comment
  • 0

It looks like you forgot to JSON-encode the payload to the POST. Try changing this line:

 

$response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body $body -ContentType "application/json"

 

to

 

$response1 = Invoke-WebRequest -Uri $requestUri -Method POST -Headers $headers -Body ($body | ConvertTo-Json)  -ContentType "application/json"

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...