Powershell ErrorSessionTokenFilter:通过HTTP标头提供的令牌与服务器生成的令牌不匹配
我试图使用Viptela的API接口从中提取事件信息。我设法获得了警报和令牌信息,但当我试图提取事件信息时,它给了我一个HTML登录页面,而不是带有事件信息的JSON 我知道,在访问事件API链接之前,我应该拥有调用事件API URL的令牌。我不确定我错过了哪一步,这对我来说不起作用。有人能指出下面代码中缺失的部分来纠正问题吗Powershell ErrorSessionTokenFilter:通过HTTP标头提供的令牌与服务器生成的令牌不匹配,powershell,rest,api,Powershell,Rest,Api,我试图使用Viptela的API接口从中提取事件信息。我设法获得了警报和令牌信息,但当我试图提取事件信息时,它给了我一个HTML登录页面,而不是带有事件信息的JSON 我知道,在访问事件API链接之前,我应该拥有调用事件API URL的令牌。我不确定我错过了哪一步,这对我来说不起作用。有人能指出下面代码中缺失的部分来纠正问题吗 if (-not ([System.Management.Automation.PSTypeName]'TrustAllCertsPolicy').Type) {
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
我得到的结果如下,我通过邮递员尝试了同样的方法,效果很好
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
请帮忙
问候,,
纳雷什找到了答案,这可能会帮助有类似问题的人 在第一个请求中,添加一个-SessionVariable参数并给它一个字符串 (没有前面$的任何字符串)然后在第二个请求中传递该字符串 (这次使用前面的$)添加到-WebSession参数 将-websession参数添加到第二个调用之后,它就工作了。(-WebSession$WebSession) 希望它能帮助别人 问候,,
Naresh通过邮递员,当我使用Powershell时,它会抛出一个错误。Invoke RestMethod:ErrorSessionTokenFilter:通过HTTP标头提供的令牌与服务器生成的令牌不匹配。
Invoke-RestMethod -Method POST -Uri $geteventurl -body $body -Headers $Headersnew -WebSession $websession