powershell - 使用 Microsoft Cognitive API 的 Invoke-RestMethod 收到 400 个错误请求错误

标签 powershell azure-cognitive-services

我们正在尝试测试和评估 Microsoft 认知服务的文本分析 API。我们正在尝试使用 Invoke-RestMethod 让快速但肮脏的 PowerShell 脚本正常工作。经过一些调整后,我们仍然收到 400 错误返回给我们。我们不确定出了什么问题,因为 JSON 似乎已更正,并且我们输入的 API key 似乎是正确的。我们利用了在另一个人的博客上发现的有关附加 header 的使用的内容,并尝试了一些变体,但仍然没有成功。有人可以为我们进行健全性检查吗?

#html tag stripper function
function htmlStrip ($results)
    {
    #using .NET toString method to ensure PS doesn't interpret same var incorrectly
    $results = $results.toString()
    $results -replace '<[^>]*(>|$)'
    }


Try
{


    [string]$sourceUrl = Read-Host "Enter a URL such as https://foobar.com"
}
Catch
{
    Write-Host "URL requires http:// or https:// prefix e.g. https://cnn.com"
}


$webClient = New-Object Net.WebClient
[string]$results = $webClient.DownloadString($sourceUrl)


[string]$cleanResults = htmlStrip $results


$body = 
[ordered]@{"documents" = 
    @{ "language" = "en"; "id" = $sourceUrl; "text" = $cleanResults }
    }

#>

$body = [ordered]@{
    "documents" = 
    @(
        @{ "language" = "en"; "id" = $sourceUrl; "text" = $cleanResults }
    )
}

$jsonBody = $body | ConvertTo-Json

#Begin Text Analytics API Call with Invoke-RestMethod wrapper
#[string]$apiUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"
[string]$apiKey = "REDACTED"


$headers = @{ "Ocp-Apim-Subscription-Key" = $apiKey }

$analyticsResults = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonBody -ContentType "application/json"  -ErrorAction Stop

Write-Host $analyticsResults

Write-Host $jsonBody

最佳答案

您放入请求的 text 属性中的数据可能无效。
我使用 GitHub 上 TypeScript 存储库的 README.md 的修复 URL 尝试了您的脚本,并且它有效。

您的脚本(稍微缩短)

$sourceUrl = 'https://raw.githubusercontent.com/Microsoft/TypeScript/master/README.md'

$webClient = New-Object Net.WebClient
$results = $webClient.DownloadString($sourceUrl)

$body = [ordered]@{
    "documents" = 
    @(
        @{ "language" = "en"; "id" = $sourceUrl; "text" = $results }
    )
}

$jsonBody = $body | ConvertTo-Json

$apiUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"
$apiKey = "..."

$headers = @{ "Ocp-Apim-Subscription-Key" = $apiKey }

$analyticsResults = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonBody -ContentType "application/json"  -ErrorAction Stop
$analyticsResults.documents.keyPhrases

结果

TypeScript compiler
gulp tests
g typescript
TypeScript source
built compiler
TypeScript users
g gulp
TypeScript directory
cd TypeScript
gulp baseline
gulp lint
gulp local
gulp clean
gulp runtests-browser
gulp LKG
Install Gulp tools
...

关于powershell - 使用 Microsoft Cognitive API 的 Invoke-RestMethod 收到 400 个错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162855/

相关文章:

powershell - 为什么我的私钥无法解密由公钥加密的 key ?

rest - 如何使用 PowerShell 和 REST API 从 TFS 2017u2 获取最新版本的代码覆盖率

powershell - 剪贴板中的文本不是数组

azure - 获取 Azure Devops Pipeline 中分支的名称

azure-cognitive-services - Microsoft 计算机视觉 OCR 读取 API 按 S3 交易而非 S2 收费

powershell - 从 Jenkins 共享文件夹中的资源文件夹调用脚本

azure - 操作返回无效状态代码 : 'NotFound'

javascript - 在Javascript中播放Azure输出格式 "audio-16khz-128kbitrate-mono-mp3"的方法

nuget-package - 无法加载DLL "Microsoft.CognitiveServices.Speech.core.dll",找不到

C# 微软认知服务登录