更新时间:2023-02-15 11:32:17
似乎在生成auth令牌时,在resourceId部分中省略了"/sprocs/{storedProcedure}".您已将其包含在uri中,这是正确的.
it seems that when you generated the auth token, in the resourceId part, you left out "/sprocs/{storedProcedure}". You have included it in the uri, which is correct.
我将附加一个示例powershell脚本,希望该脚本还将帮助您了解如何生成身份验证令牌.
I am attaching a sample powershell script that, hopefully, will also help you to see how auth token is generated.
Add-Type -AssemblyName System.Web
$accountName = "<db account name>"
$connectionKey = "<secret key>"
$collectionName = "<coll name>"
$databaseName = "<db name>"
Write-host ("Account " + $accountName)
Write-host ("Database " + $databaseName)
Write-host ("Collection " + $collectionName)
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)
Write-Host($text)
[System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
}
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"
$headers = BuildHeaders -resType colls -resourceId $dbname
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
$response.DocumentCollections
Write-Host ("Found " + $Response.DocumentCollections.Count + " DocumentCollection(s)")
}
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 PostDocument([string]$document, [string]$dbname, [string]$collection){
$collName = "dbs/"+$dbname+"/colls/" + $collection
$headers = BuildHeaders -action Post -resType docs -resourceId $collName
$headers.Add("x-ms-documentdb-is-upsert", "true")
$uri = $rootUri + "/" + $collName + "/docs"
Write-host ("Calling " + $uri)
$response = Invoke-RestMethod $uri -Method Post -Body $json -ContentType 'application/json' -Headers $headers
$response
}
function PostSprocQuery([string]$dbname, [string]$collection){
$sprocName = "dbs/"+$dbname+"/colls/" + $collection + "/sprocs/samplesproc"
$headers = BuildHeaders -action Post -resType sprocs -resourceId $sprocName
$uri = $rootUri + "/" + $sprocName
Write-host ("Calling " + $uri)
write-host $authz
write-host $apiDate
$response = Invoke-RestMethod $uri -Method Post -Body $json -ContentType 'application/json' -Headers $headers
$response
}
$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
}
$json = @"
{
"id": "3"
}
"@
PostDocument -document $json -dbname $databaseName -collection $collectionName
$json = @"
[
"samplesproc"
]
"@
PostSprocQuery -document $json -dbname $databaseName -collection $collectionName