Azure DocumentDB Rest API PowerShell删除集合401未初始化
需要在我的自动化过程中删除集合。 正在尝试执行下面的脚本。Get操作工作正常,但删除操作失败,出现“(401)Unahorized”错误。奇怪的是,删除集合不需要额外的头。有人能给个提示吗,怎么了Azure DocumentDB Rest API PowerShell删除集合401未初始化,rest,powershell,azure,azure-cosmosdb,Rest,Powershell,Azure,Azure Cosmosdb,需要在我的自动化过程中删除集合。 正在尝试执行下面的脚本。Get操作工作正常,但删除操作失败,出现“(401)Unahorized”错误。奇怪的是,删除集合不需要额外的头。有人能给个提示吗,怎么了 $accountName = 'someaccountname' $connectionKey = 'masterkey' $collectionName = 'mycollection' $databaseName = 'mydatabase' function GetKey([System.S
$accountName = 'someaccountname'
$connectionKey = 'masterkey'
$collectionName = 'mycollection'
$databaseName = 'mydatabase'
function GetKey([System.String]$Verb = '',[System.String]$ResourceId = '',
[System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') {
$keyBytes = [System.Convert]::FromBase64String($masterKey)
$text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "`n")
$body =[Text.Encoding]::UTF8.GetBytes($text)
$hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes)
$hash = $hmacsha.ComputeHash($body)
$signature = [System.Convert]::ToBase64String($hash)
[System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
}
function BuildHeaders([string]$action = "get",[string]$resType, [string]$resourceId){
$authz = GetKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $connectionKey
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authz)
$headers.Add("x-ms-version", '2015-12-16')
$headers.Add("x-ms-date", $apiDate)
$headers
}
function GetUTDate() {
$date = get-date
$date = $date.ToUniversalTime();
return $date.ToString("r", [System.Globalization.CultureInfo]::InvariantCulture);
}
function GetDatabases() {
$uri = $rootUri + "/dbs"
$hdr = BuildHeaders -resType dbs
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $hdr
$response.Databases
Write-Host ("Found " + $Response.Databases.Count + " Database(s)")
}
function GetCollections([string]$dbname){
$uri = $rootUri + "/" + $dbname + "/colls"
$hdr = BuildHeaders -resType colls -resourceId $dbname
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $hdr
$response.DocumentCollections
Write-Host ("Found " + $Response.DocumentCollections.Count + " DocumentCollection(s)")
}
function DeleteCollection([string]$dbname){
$uri = $rootUri + "/" + $dbname + "/colls" + "/" + $collectionName
$hdrs = BuildHeaders -action DELETE -resType colls -resourceId $collectionName
$response = Invoke-RestMethod -Uri $uri -Method Delete -Headers $hdrs
Write-Host "DELETE $uri"
}
$rootUri = "https://" + $accountName + ".documents.azure.com"
write-host ("Root URI is " + $rootUri)
#validate arguments
$apiDate = GetUTDate
$db = GetDatabases | where { $_.id -eq $databaseName }
if ($db -eq $null) {
write-error "Could not find database in account"
return
}
$dbname = "dbs/" + $databaseName
$collection = GetCollections -dbname $dbname | where { $_.id -eq $collectionName }
if($collection -eq $null){
write-error "Could not find collection in database"
return
}
Write-Host
$Delete = DeleteCollection -dbname $dbname | where { $_.id -eq $collectionName }
如果存在挂起的操作,则可能发生这种情况。请重试直到成功,或者在删除之前添加延迟。对我们来说,半秒钟就足以保证我们再也看不到这种情况了 如果存在挂起的操作,则可能发生这种情况。请重试直到成功,或者在删除之前添加延迟。对我们来说,半秒钟就足以保证我们再也看不到这种情况了 通常,将返回未设置授权或x-ms-date头(或带有无效授权令牌的授权头)。我使用fiddler捕获请求并检查响应,我发现Authorization和x-ms-date头都已设置,因此Authorization头似乎被设置为无效的授权令牌。根据您的代码,我做了一些更改,该函数在我这边可以正常工作
function DeleteCollection([string]$dbname){
$uri = $rootUri + "/" + $dbname + "/colls" + "/" + $collectionName
$collectionName = $dbname + "/colls" + "/" + $collectionName
$hdrs = BuildHeaders -action DELETE -resType colls -resourceId $collectionName
#Write-Host "resourceId $collectionName"
$response = Invoke-RestMethod -Uri $uri -Method Delete -Headers $hdrs
Write-Host "resource $collectionName"
Write-Host "DELETE $uri"
}
$collectionName应该是dbs/{yourdbname}/colls/{yourcollectionname}
通常,将返回未设置授权或x-ms-date头(或带有无效授权令牌的授权头)。我使用fiddler捕获请求并检查响应,我发现Authorization和x-ms-date头都已设置,因此Authorization头似乎被设置为无效的授权令牌。根据您的代码,我做了一些更改,该函数在我这边可以正常工作
function DeleteCollection([string]$dbname){
$uri = $rootUri + "/" + $dbname + "/colls" + "/" + $collectionName
$collectionName = $dbname + "/colls" + "/" + $collectionName
$hdrs = BuildHeaders -action DELETE -resType colls -resourceId $collectionName
#Write-Host "resourceId $collectionName"
$response = Invoke-RestMethod -Uri $uri -Method Delete -Headers $hdrs
Write-Host "resource $collectionName"
Write-Host "DELETE $uri"
}
$collectionName应该是dbs/{yourdbname}/colls/{yourcollectionname}