json - Invoke-WebRequest : Unauthorized - 请求需要用户身份验证
问题描述
我正在点击这个网址
https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login
然后我得到会话并传递到下面的 url
https://myistra.td90.centile-dev.com/restleroute/v1/3rdParty/AdmtiveDomain。
问题
输出
Invoke-WebRequest : Unauthorized
The request requires user authentication
You can get technical details here.
Please continue your visit at our home page.
At C:\Users\administrator\Documents\CDR.ps1:20 char:213
+ ... 'Session' | Invoke-WebRequest -Uri ("https://myistra.td90.centile-
dev ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation:
(System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
thirdParty_SESSIONID=969514241160310577; _ac958=http://10.20.100.190:8078
这是我的代码
$user = "SuperGabriel"
$pass = "SuperGabriel@2019"
$pair = "$($user):$($pass)"
$encodedCreds =
$basicAuthValue = "Basic $encodedCreds"
$PartOneHeaders = @{
"Authorization" = $basicAuthValue
"X-Application" = "3rdParty"
}
#test
$Headers =@{
"X-Application" = "3rdParty"
"Cookie"= "thirdParty_SESSIONID=7353595784495763113;"
}
$PartOneWebRequest = Invoke-WebRequest -Uri https://webadmin.td90.centile-
dev.com/restletrouter/v1/service/Login -Headers $PartOneHeaders -
ContentType "application/json" -Method POST -SessionVariable 'Session' |
Invoke-WebRequest -Uri ("https://myistra.td90.centile-
dev.com/restleroute/v1/3rdParty/AdmtiveDomain" ) -Headers $Headers -
Method Get
我已经回应了所有内容,以确保我获得正确的信息并将其传递到正确的地方。我已经在邮递员中测试了这个电话,它可以工作。
预期成绩
[
{
"restUri": "v1/3rdParty/AdmtiveDomain/0.",
"alias": "TopLevelAdmtiveDomain",
"rootModel": "AdmtiveDomain",
"domainName": "Top-Level",
"admtiveDomainID": "0."
},
{
"restUri": "v1/3rdParty/AdmtiveDomain/0.106.",
"alias": "AdmtiveDomainSpecific",
"rootModel": "AdmtiveDomain",
"domainName": "acd-00",
"domainType": "Enterprise",
"admtiveDomainID": "0.106."
}
]
解决方案
[System.Uri]$Uri = "https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login" # Add the Uri
[System.Uri]$Uri_Get = "https://webadmin.td90.centile-dev.com/restletrouter/v1/3rdParty/AdmtiveDomain"
$ContentType = "application/json" # Add the content type
$Method = 'POST' # Add the method type
$user = "SuperGabriel"
$pass = "SuperGabriel@2019"
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
# Add name and value of headers
"Authorization" = $basicAuthValue
"Cookie" = "thirdParty_SESSIONID=6335513255659796064"
"X-Application" = "3rdParty"
}
$Headers_Get = @{
# Add name and value of headers
"Cookie" = "thirdParty_SESSIONID=4436753218874838616; thirdParty_SESSIONID=6335513255659796064;"
"X-Application" = "3rdParty"
"Host" = "webadmin.td90.centile-dev.com"
"Content-Type"= "$ContentType"
}
$Headers_Get2 = @{
# Add name and value of headers
"Cookie" = "thirdParty_SESSIONID=1312545750448673312"
"X-Application" = "3rdParty"
"Host" = "webadmin.td90.centile-dev.com"
"Content-Type"= "$ContentType"
}
# Splat the parameters
$props = @{
Uri = $Uri
Headers = $Headers
ContentType = $ContentType
Method = $Method
}
$props_Get = @{
Uri = $Uri_Get
Headers = $Headers_Get
ContentType = $ContentType
Method = "GET"
}
try
{
$Result= Invoke-RestMethod @props -SessionVariable sess -OutFile C:\output.json
echo $Result
$Result1= Invoke-RestMethod @props_Get -OutFile C:\"$DateStr".json -WebSession $sess
echo $Result1
}
catch [System.Net.WebException]
{
$res = $_.Exception.Response
echo $res
}
将根据您的要求工作。
干杯..
推荐阅读
- r - 如何根据另一个数据集中的值过滤一个数据集中的日期和时间
- selenium - 无法在 QAF 中实现 DataTable 黄瓜功能
- amazon-web-services - 如何使用 CLI 获取任何给定区域的所有 pod 详细信息
- python - 如何使用 ModbusTCP 协议将 sensor_1 和 sensor_2 的值从 slave-PC 传输到 master-PC?
- r - R - 结合两个数据表元素的简单方法是什么?
- javascript - onclick,按钮向下移动,为什么会这样?
- android - Android Compose-Navigation 2.4.0-alpha2 崩溃并出现 NoSuchMethodError
- flutter - 无法根据设备大小初始化变量
- security - 没有登录页面的网站之间的授权
- javascript - 如何停止节点js中代码的执行而不是返回