无法使用powershell rest API从波斯菊获取文档数

问题描述 投票:0回答:1

[尝试使用Rest API使用Powershell查询Cosmoshttps://docs.microsoft.com/en-us/rest/api/cosmos-db/query-documents我收到以下错误

Invoke-RestMethod:远程服务器返回错误:(400)错误要求

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)

    If ($resourceLink -eq $resourceType) {
        $resourceLink = ""
    }

    $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 Query-CosmosDocuments{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)][String]$EndPoint,
        [Parameter(Mandatory=$true)][String]$DBName,
        [Parameter(Mandatory=$true)][String]$CollectionName,
        [Parameter(Mandatory=$true)][String]$MasterKey,
        [Parameter(Mandatory=$false)][String]$JSON,      
        [String]$Verb="POST"
    )
    $ResourceType = "docs";
    $ResourceLink = "dbs/$DBName/colls/$CollectionName"
$query=@"
{  
  "query": "SELECT VALUE COUNT(1) FROM c",  
  "parameters": [  ]  
} 
"@

#$query= "SELECT VALUE COUNT(1) FROM c"

    $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-documentdb-isquery"="True";"x-ms-date"=$dateTime}
    $contentType= "application/query+json"
    $queryUri = "$EndPoint$ResourceLink/docs"
    #$header
    #$queryUri

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $result = Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $query 
    return $result
}

Add-Type -AssemblyName System.Web
$CosmosDBEndPoint = "https://Mydev.documents.azure.com:443/"
$DBName = "MyDev"
$CollectionName = "ArchiveSummary"
$MasterKey = "MyMasterKey"

Query-CosmosDocuments -EndPoint $CosmosDBEndPoint -DBName $DBName -CollectionName $CollectionName -MasterKey $MasterKey

Cosmos创建脚本

$resourceGroupName='mytestcosmos'
$accountName='testcosmosaccount' 
az group create -l westus -n $resourceGroupName
az cosmosdb create --name $accountName --resource-group $resourceGroupName 


$databaseName='database1'

az cosmosdb database create -n $accountName -g $resourceGroupName -d $databaseName --throughput 1000

$collectionName = 'collection1'
$partitionKey = '/name'

az cosmosdb collection create -g $resourceGroupName -n $accountName -d $databaseName -c $collectionName --partition-key-path $partitionKey
powershell azure-cosmosdb
1个回答
0
投票

根据我的测试,如果要使用PowerShell调用Azure Cosmos DB rest api,请参考以下脚本

$Key="<your master key>"
$Verb="POST"
$ResourceType="docs"
$ResourceLink ="dbs/ToDoList/colls/Items"
$Date= (Get-Date).ToUniversalTime().toString('R')
$KeyType="master"
$TokenVersion="1.0"

# create Authorization header
$StringToSign=$Verb.ToLowerInvariant()+"`n" +$ResourceType.ToLowerInvariant() +"`n" +$ResourceLink + "`n" +$Date.ToLowerInvariant() + "`n" + "" + "`n";
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Convert]::FromBase64String($Key)
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($StringToSign))
$signature = [Convert]::ToBase64String($signature)

$authorization = [System.Web.HttpUtility]::UrlEncode("type=${KeyType}&ver=${TokenVersion}&sig=$signature")

#call rest api
$query=@"
{  
  "query": "SELECT * FROM c",  
  "parameters": [  ]  
} 
"@

$header = @{Authorization=$authorization;"x-ms-version"="2017-02-22";"x-ms-documentdb-isquery"="True";"x-ms-date"=$Date}
$contentType= "application/query+json"
$CosmosDBEndPoint="https://stancosmosdb.documents.azure.com/"
$url=$CosmosDBEndPoint+$ResourceLink+"/docs"
Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $url -Headers $header -Body $query 

有关更多详细信息,请参阅

https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources

https://docs.microsoft.com/en-us/rest/api/cosmos-db/query-documents

© www.soinside.com 2019 - 2024. All rights reserved.