使用 PowerShell 5 作为 Kentico 8.2 REST 客户端时出现 403 错误
403 Error using PowerShell 5 as Kentico 8.2 REST client
当我在同时登录到 Kentico 8.2 管理站点的浏览器中直接加载它时,此 REST 请求有效:
https://www.example.com/rest/CMS.SettingsKey
现在,我需要使用 PowerShell 5 return 相同的结果。我尝试了以下各种版本:
$url = "https://www.example.com/rest/CMS.SettingsKey"
$httpMethod = "Get"
$credentialsBytes = [System.Text.Encoding]::UTF8.GetBytes("username:password")
$credentialsEncoded = [System.Convert]::ToBase64String($credentialsBytes)
$headers = @{}
$headers.Add("Authorization", "Basic $($credentialsEncoded)")
$settings = (Invoke-RestMethod -Uri $url -Method $httpMethod -headers $headers)
Write-Host $settings
请注意,"username" 和 "password" 是用于登录管理站点的相同凭据(当 REST 请求在浏览器中运行时),并且用户是全局管理员。
PS 代码段给我一个 403 禁止错误。我关注了 this page and this page,但无法正常使用。我做错了什么?
编辑:
我启用了 REST 服务,但现在收到 401 Unauthorized 错误。同样,用户是全局管理员。我感觉 headers 没有包含在请求中(或者 headers 有问题),因为如果我生成哈希并使用相同的请求(来自 PowerShell)散列参数认证代替基本认证。我尝试按照评论中的建议使用 Fiddler,但我是新手,现在没有时间深入研究。
刚刚使用最新的修补程序版本(9.0.32 和 8.2.48)对其进行了测试,它工作正常。
- 确保为特定站点启用了 REST(不仅是全局设置)
- 确保您的 web.config 中有
<modules runAllManagedModulesForAllRequests="true">
,如 here 所述。
当我在同时登录到 Kentico 8.2 管理站点的浏览器中直接加载它时,此 REST 请求有效:
https://www.example.com/rest/CMS.SettingsKey
现在,我需要使用 PowerShell 5 return 相同的结果。我尝试了以下各种版本:
$url = "https://www.example.com/rest/CMS.SettingsKey"
$httpMethod = "Get"
$credentialsBytes = [System.Text.Encoding]::UTF8.GetBytes("username:password")
$credentialsEncoded = [System.Convert]::ToBase64String($credentialsBytes)
$headers = @{}
$headers.Add("Authorization", "Basic $($credentialsEncoded)")
$settings = (Invoke-RestMethod -Uri $url -Method $httpMethod -headers $headers)
Write-Host $settings
请注意,"username" 和 "password" 是用于登录管理站点的相同凭据(当 REST 请求在浏览器中运行时),并且用户是全局管理员。
PS 代码段给我一个 403 禁止错误。我关注了 this page and this page,但无法正常使用。我做错了什么?
编辑:
我启用了 REST 服务,但现在收到 401 Unauthorized 错误。同样,用户是全局管理员。我感觉 headers 没有包含在请求中(或者 headers 有问题),因为如果我生成哈希并使用相同的请求(来自 PowerShell)散列参数认证代替基本认证。我尝试按照评论中的建议使用 Fiddler,但我是新手,现在没有时间深入研究。
刚刚使用最新的修补程序版本(9.0.32 和 8.2.48)对其进行了测试,它工作正常。
- 确保为特定站点启用了 REST(不仅是全局设置)
- 确保您的 web.config 中有
<modules runAllManagedModulesForAllRequests="true">
,如 here 所述。