The remote server returned an error: (400) Bad Request on invoke-restmethod using powershell
The remote server returned an error: (400) Bad Request on invoke-restmethod using powershell
我正在尝试使用 Powershell 进行 api 调用,这是脚本
$clientID = "xxxxxxxxxxxxx"
$tenantName = "xxxxxxxxxxxxxxxxxxx"
$ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXx"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientID
Client_Secret = $clientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$authheader = @{
'Authorization' = "Bearer $($Tokenresponse.access_token)"
}
$ssoPatchUri = 'https://graph.microsoft.com/v1.0/applications/xxxxxxxxxxxxx-b64417d8183c'
$body = @'
{
"web": @{"redirectUris" = @("https://signin.aws.amazon.com/saml")}
"identifierUris" : @("https://signin.aws.amazon.com/saml")
}
'@
Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Body $body -Method Patch -ContentType 'application/json' -Verbose
在我们传递$body 参数的最后一个invoke-restmethod 中发生错误,我认为这是由于嵌套json 的不正确框架造成的。
这是我得到的错误。(因为我没有复制整个代码,行号将无效)
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\user\test.ps1:77 char:14
+ ... $final = Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
这是我需要传递的实际 json 数据,
{
"web": {
"redirectUris": [
"https://signin.aws.amazon.com/saml"
]
},
"identifierUris": [
"https://signin.aws.amazon.com/saml"
]
}
如果有人能帮我解决这个问题,我将不胜感激
我已经使用你的代码重现了这个问题。
已走步数。
我已同意以下权限:Application.ReadWrite.All、Directory.ReadWrite.All、Application.ReadWrite.OwnedBy
我稍微修改了正文中的代码,并将内容类型放在 authheader 中。
$TenantName = "****.onmicrosoft.com"
$clientID = "**********"
$clientSecret = "*****************"
$Scope = "https://graph.microsoft.com/.default"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = $Scope
client_Id = $clientID
Client_Secret = $clientSecret
}
$authUri = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
$TokenResponse = Invoke-RestMethod -Uri $authUri -Method POST -Body $ReqTokenBody
$authheader = @{
"Authorization" = "Bearer $($Tokenresponse.access_token)"
"Content-type" = "application/json"
}
$TokenResponse.access_token
$ssoPatchUri = 'https://graph.microsoft.com/v1.0/applications/####’
$body = '{
"web":
{
"redirectUris": [
"https://signin.aws.amazon.com/saml"
]
},
"identifierUris" : ["https://signin.aws.amazon.com/saml"]
}'
Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Method PATCH -Body $body
#$v=Invoke-RestMethod -Headers $authheader -Uri "https://graph.microsoft.com/v1.0/applications/#######" -Method GET
#$v
#$v.web
我在这里放置了 api 的 object id 而不是客户端 id(在 -Uri https://graph.microsoft.com/v1.0/applications/objectId 中)来执行 PATCH 请求,它 worked.It 在使用客户端 ID 时显示相同的错误。
但是使用客户端 ID(https://graph.microsoft.com/v1.0/applications/ClientId) 对 GET 请求有效。
重定向 uri 和标识符 uri 在使用 uri 中的对象 id 后成功更新到 api 可以使用 GET 请求显示补丁。
我正在尝试使用 Powershell 进行 api 调用,这是脚本
$clientID = "xxxxxxxxxxxxx"
$tenantName = "xxxxxxxxxxxxxxxxxxx"
$ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXx"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientID
Client_Secret = $clientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$authheader = @{
'Authorization' = "Bearer $($Tokenresponse.access_token)"
}
$ssoPatchUri = 'https://graph.microsoft.com/v1.0/applications/xxxxxxxxxxxxx-b64417d8183c'
$body = @'
{
"web": @{"redirectUris" = @("https://signin.aws.amazon.com/saml")}
"identifierUris" : @("https://signin.aws.amazon.com/saml")
}
'@
Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Body $body -Method Patch -ContentType 'application/json' -Verbose
在我们传递$body 参数的最后一个invoke-restmethod 中发生错误,我认为这是由于嵌套json 的不正确框架造成的。 这是我得到的错误。(因为我没有复制整个代码,行号将无效)
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\user\test.ps1:77 char:14
+ ... $final = Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
这是我需要传递的实际 json 数据,
{
"web": {
"redirectUris": [
"https://signin.aws.amazon.com/saml"
]
},
"identifierUris": [
"https://signin.aws.amazon.com/saml"
]
}
如果有人能帮我解决这个问题,我将不胜感激
我已经使用你的代码重现了这个问题。
已走步数。
我已同意以下权限:Application.ReadWrite.All、Directory.ReadWrite.All、Application.ReadWrite.OwnedBy
我稍微修改了正文中的代码,并将内容类型放在 authheader 中。
$TenantName = "****.onmicrosoft.com"
$clientID = "**********"
$clientSecret = "*****************"
$Scope = "https://graph.microsoft.com/.default"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = $Scope
client_Id = $clientID
Client_Secret = $clientSecret
}
$authUri = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
$TokenResponse = Invoke-RestMethod -Uri $authUri -Method POST -Body $ReqTokenBody
$authheader = @{
"Authorization" = "Bearer $($Tokenresponse.access_token)"
"Content-type" = "application/json"
}
$TokenResponse.access_token
$ssoPatchUri = 'https://graph.microsoft.com/v1.0/applications/####’
$body = '{
"web":
{
"redirectUris": [
"https://signin.aws.amazon.com/saml"
]
},
"identifierUris" : ["https://signin.aws.amazon.com/saml"]
}'
Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Method PATCH -Body $body
#$v=Invoke-RestMethod -Headers $authheader -Uri "https://graph.microsoft.com/v1.0/applications/#######" -Method GET
#$v
#$v.web
我在这里放置了 api 的 object id 而不是客户端 id(在 -Uri https://graph.microsoft.com/v1.0/applications/objectId 中)来执行 PATCH 请求,它 worked.It 在使用客户端 ID 时显示相同的错误。
但是使用客户端 ID(https://graph.microsoft.com/v1.0/applications/ClientId) 对 GET 请求有效。
重定向 uri 和标识符 uri 在使用 uri 中的对象 id 后成功更新到 api 可以使用 GET 请求显示补丁。