【问题标题】:using Invoke-RestMethod to post to a cosmosDB Returns a 400使用 Invoke-RestMethod 发布到 cosmosDB 返回 400
【发布时间】:2019-10-25 15:49:12
【问题描述】:

我正在尝试使用 powershell 将文档发布到 cosmosdb,我已按照本文https://www.systemcenterautomation.com/2018/06/cosmos-db-rest-api-powershell/ 中的步骤进行操作,但每次仍然出现 400 错误

Add-Type -AssemblyName System.Web

# generate authorization key
Function Generate-MasterKeyAuthorizationSignature
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$verb,
        [Parameter(Mandatory=$true)][String]$resourceLink,
        [Parameter(Mandatory=$true)][String]$resourceType,
        [Parameter(Mandatory=$true)][String]$dateTime,
        [Parameter(Mandatory=$true)][String]$key,
        [Parameter(Mandatory=$true)][String]$keyType,
        [Parameter(Mandatory=$true)][String]$tokenVersion
    )

    $hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256
    $hmacSha256.Key = [System.Convert]::FromBase64String($key)

    $payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n"
    $hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad))
    $signature = [System.Convert]::ToBase64String($hashPayLoad);

    [System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature")
} 


Function Post-CosmosDocuments{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$EndPoint,
        [Parameter(Mandatory=$true)][String]$DBName,
        [Parameter(Mandatory=$true)][String]$CollectionName,
        [Parameter(Mandatory=$true)][String]$MasterKey,
        [String]$Verb="POST",
        [Parameter(Mandatory=$true)][String]$JSON
    )
    $Verb = "POST"
    $ResourceType = "docs";
    $ResourceLink = "dbs/$DBName/colls/$CollectionName"

    $dateTime = [DateTime]::UtcNow.ToString("r")
    $authHeader = Generate-MasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
    $header = @{authorization=$authHeader;"x-ms-version"="2017-02-22";"x-ms-date"=$dateTime}
    $contentType= "application/json"
    $queryUri = "$EndPoint$ResourceLink/docs"

    $result = Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $JSON
    return $result.statuscode
} 


$CosmosDBEndPoint = "https://<omitted>.documents.azure.com:443/"
$DBName = "database"
$CollectionName = "container"
$MasterKey = "<omitted>=="

$SomeObject = [PSObject]@{ id = 1 ; Application = "Ops"; Environment = "Dev"; adKey = "555-555-5555"; } 
Post-CosmosDocuments -EndPoint $CosmosDBEndPoint -MasterKey $MasterKey -DBName $DBName -CollectionName $CollectionName -JSON ($SomeObject | ConvertTo-Json)

每次都返回 400,我不确定请求有什么问题。

【问题讨论】:

  • 它是一个新集合还是您正在更新现有集合?
  • 可以添加响应正文吗?
  • 这可能是我没有创建一个集合,它正在工作

标签: powershell azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

基于Powershell script samples,我发现您缺少标题:

  • x-ms-documentdb-partitionkey
  • 用户代理
  • 2018-12-31 为 x-ms-version

添加您获得的响应正文也会有所帮助。

【讨论】:

    【解决方案2】:

    它正在工作

    我不知道为什么我得到了 400。也许我创建集合时的时间已经关闭。

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多