Cosmos DB REST API 授权 header 不允许我替换文档
Cosmos DB REST API authorisation header won't allow me to replace a document
我正在编写一个 PowerShell 脚本来读取和修改 Cosmos DB 中的文档 collection。对于阅读,我正在使用 GET endpoint, and for modification I'm using the PUT endpoint.
读取工作正常,但对于修改,我得到 401,没有任何有用的解释。
这是我正在使用的生成授权 header 的代码,正如我所说,它可以正常读取:
Function New-CosmosAuthorisationToken {
Param([String]$Verb, [String]$ResourceType, [String]$ResourceId, [String]$Date, [String]$Key, [String]$KeyType, [String]$TokenVersion)
$convertedKey = [Convert]::FromBase64String($Key)
$hmacSha256 = New-Object -TypeName System.Security.Cryptography.HMACSHA256
$hmacSha256.Key = $convertedKey
$payload = [String]::Format([System.Globalization.CultureInfo]::InstalledUICulture, "{0}`n{1}`n{2}`n{3}`n{4}`n",$verb.ToLowerInvariant(),$resourceType.ToLowerInvariant(),$resourceId,$date.ToLowerInvariant(),"")
$hashPayload = $hmacSha256.ComputeHash([Text.Encoding]::UTF8.GetBytes($payload))
$signature = [Convert]::ToBase64String($hashPayload)
$header = [String]::Format([System.Globalization.CultureInfo]::InstalledUICulture,"type={0}&ver={1}&sig={2}",$KeyType,$TokenVersion,$Signature)
[System.Web.HttpUtility]::UrlEncode($header)
}
当我生成用于修改的令牌时,我是:
- 将函数调用中的 $Verb 参数设置为 PUT
- 使用 read-write(相对于只读)访问密钥
我用于修改的 URI 与在 Azure 门户中成功修改的 URI 完全匹配(您可以使用浏览器开发工具检查它们)。
我在请求中包含的 header 是:
- 授权:(令牌)
- Content-Type:(application/json,相对于读取调用的 application/query+json)
- x-ms-version: (2018-12-31)
- x-ms-date:(现在为 UTC,与用于生成授权 header 令牌的日期完全相同)
- x-ms-documentdb-partitionkey:文档的分区键,在数组中单独存在
谁能想象为什么我会从 REST 返回 401 API?
因为我现在正在更新一个特定的命名文档,所以传递给函数以生成授权 header 令牌的 ResourceId
应该标识特定的文档,例如
dbs/mydatabase/colls/mycollection/docs/mydocumentid
我正在编写一个 PowerShell 脚本来读取和修改 Cosmos DB 中的文档 collection。对于阅读,我正在使用 GET endpoint, and for modification I'm using the PUT endpoint.
读取工作正常,但对于修改,我得到 401,没有任何有用的解释。
这是我正在使用的生成授权 header 的代码,正如我所说,它可以正常读取:
Function New-CosmosAuthorisationToken {
Param([String]$Verb, [String]$ResourceType, [String]$ResourceId, [String]$Date, [String]$Key, [String]$KeyType, [String]$TokenVersion)
$convertedKey = [Convert]::FromBase64String($Key)
$hmacSha256 = New-Object -TypeName System.Security.Cryptography.HMACSHA256
$hmacSha256.Key = $convertedKey
$payload = [String]::Format([System.Globalization.CultureInfo]::InstalledUICulture, "{0}`n{1}`n{2}`n{3}`n{4}`n",$verb.ToLowerInvariant(),$resourceType.ToLowerInvariant(),$resourceId,$date.ToLowerInvariant(),"")
$hashPayload = $hmacSha256.ComputeHash([Text.Encoding]::UTF8.GetBytes($payload))
$signature = [Convert]::ToBase64String($hashPayload)
$header = [String]::Format([System.Globalization.CultureInfo]::InstalledUICulture,"type={0}&ver={1}&sig={2}",$KeyType,$TokenVersion,$Signature)
[System.Web.HttpUtility]::UrlEncode($header)
}
当我生成用于修改的令牌时,我是:
- 将函数调用中的 $Verb 参数设置为 PUT
- 使用 read-write(相对于只读)访问密钥
我用于修改的 URI 与在 Azure 门户中成功修改的 URI 完全匹配(您可以使用浏览器开发工具检查它们)。
我在请求中包含的 header 是:
- 授权:(令牌)
- Content-Type:(application/json,相对于读取调用的 application/query+json)
- x-ms-version: (2018-12-31)
- x-ms-date:(现在为 UTC,与用于生成授权 header 令牌的日期完全相同)
- x-ms-documentdb-partitionkey:文档的分区键,在数组中单独存在
谁能想象为什么我会从 REST 返回 401 API?
因为我现在正在更新一个特定的命名文档,所以传递给函数以生成授权 header 令牌的 ResourceId
应该标识特定的文档,例如
dbs/mydatabase/colls/mycollection/docs/mydocumentid