title: ErrorSessionTokenFilter: Token provided via HTTP Header does not match the token generated by the server
body: <p>I am trying to extract Event information from Viptela using its API interface. I managed to get alert and token information but when I tried to extract Event information, it gave me an HTML login page instead of JSON with event information.</p>
  
  <p>I came to know that before accessing the Event API link I should have the token to call Event API URL. I am not sure which step I have missed and it is not working for me. Could someone point out the missing piece from the code below to rectify the issue?</p>
  
  <pre><code>    if (-not ([System.Management.Automation.PSTypeName]'TrustAllCertsPolicy').Type) {
      Add-Type -TypeDefinition @"
          using System.Net;
          using System.Security.Cryptography.X509Certificates;
          public class TrustAllCertsPolicy : ICertificatePolicy {
              public bool CheckValidationResult(
                  ServicePoint srvPoint, X509Certificate certificate,
                  WebRequest request, int certificateProblem) {
                  return true;
              }
          }
  "@
  }
  [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy -ErrorAction SilentlyContinue
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  
  $Authorization = [Convert]::ToBase64String(
      [Text.Encoding]::ASCII.GetBytes(
          (
              "{0}:{1}" -f ('UserName' , 'Password')
          )
      )
  )
  
  $Headers = @{"authorization" = "Basic "+$Authorization}
  
  
  $Uri = "https://vmanage.viptela.net:443/dataservice/client/token"
  $gettoken = Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers -SessionVariable websession
  $gettoken.Cookies
  $cookie = ($websession.Cookies.GetCookies($Uri)).Value
  
  $body = '{
      "query": {
          "condition": "AND",
          "rules": [
              {
                  "value": [
                      "1"
                  ],
                  "field": "entry_time",
                  "type": "date",
                  "operator": "last_n_hours"
              }
          ]
      }
  }'
  
  $Headersnew = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  $Headersnew.Add("X-XSRF-TOKEN", $gettoken)
  $Headersnew.Add("Authorization", "Basic "+$Authorization)
  $Headersnew.Add("Content-Type", "application/json")
  $Headersnew.Add("Cookie", "JSESSIONID="+$cookie)
  
  
  $geteventurl = "https://vmanage.viptela.net:443/dataservice/event"
  
  $getevent = Invoke-RestMethod -Method POST -Uri $geteventurl -body $body -Headers $Headersnew 
  $getevent
  </code></pre>
  
  <p>The result I got is given below, I tried the same via PostMan it works fine.</p>
  
  <pre><code>Invoke-RestMethod : ErrorSessionTokenFilter: Token provided via HTTP Header does not match the token generated by the server.
  At C:\Users\user\Desktop\00viptela\GetEvents.ps1:58 char:13
  + $getevent = Invoke-RestMethod -Method POST -Uri $geteventurl -body $b ...
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
  </code></pre>
  
  <p>Please help.</p>
  
  <p>Regards,
  Naresh</p>