Invoke-WebRequest:未经授权-该请求需要用户身份验证

时间:2019-04-28 17:12:29

标签: json rest powershell http header

我点击了这个网址

https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login

然后我要进行会话并传递到下面的网址

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."
}
]

1 个答案:

答案 0 :(得分:0)

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

将按照您的要求工作。
干杯 ..

相关问题