首页 > 解决方案 > 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."
}
]

标签: jsonrestpowershellhttpheader

解决方案


[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
}

将根据您的要求工作。
干杯..


推荐阅读