azure - 如何修复 PUT Invoke-RestMethod 中的 "401 Unauthorized"错误(尝试替换存储过程)

标签 azure azure-cosmosdb azure-powershell

我正在尝试使用 REST API 和 PowerShell 管理 CosmosDB 资源。 当我使用 GET 和 POST 时,我的代码工作正常,但是当我尝试使用 PUT 替换现有对象时,出现 401 错误。

我的代码在存储过程不存在时注册它,并在 SP 已存在时更新它。

因此,当我创建新的 SP 时,我使用以下变量:

$Verb = "POST"
$ResourceType = "sprocs"
$ResourceLink = "dbs/$DBName/colls/$CollName"
$queryUri = "$CosmosDBEndPoint$ResourceLink/$ResourceType"

获取身份验证 header :

...
$authHeader = Generate-MasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime

然后调用REST方法:

$header = @{authorization=$authHeader;"x-ms-version"="2017-02-22";"x-ms-date"=$dateTime} 
$contentType= "application/json"
Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $body

它工作得很好并且创造了它应该做的事情。 而且,当我需要替换现有对象(在我的例子中是 SP)时,我会像这样更改变量:

$Verb = "PUT"
$ResourceType = "sprocs"
$ResourceLink = "dbs/$DBName/colls/$CollName"
$ItemName = "SP_Name"
$queryUri = "$CosmosDBEndPoint$ResourceLink/$ResourceType/$ItemName"

生成 auth header ,就像 POST 的情况一样(只有动词不同):

...
$authHeader = Generate-MasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
...

并使用新的 URI 调用 REST 方法(其中现在包括要更改的 SP 名称):

$header = @{authorization=$authHeader;"x-ms-version"="2017-02-22";"x-ms-date"=$dateTime} 
$contentType= "application/json"
Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $body

这会抛出401 Unauthorized,所以看来 auth header 不正确。不知道我应该在那里改变什么。

最佳答案

从这里看来,您尝试更改的资源或资源中的值不支持 PUT 操作。您可以手动确认;

https://learn.microsoft.com/en-us/rest/api/cosmos-db/

或前往;

https://resources.azure.com/

关于azure - 如何修复 PUT Invoke-RestMethod 中的 "401 Unauthorized"错误(尝试替换存储过程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55502404/

相关文章:

powershell - 如何下载特定订阅的 Azure PublishSettings 文件

azure - 通过 Powershell 添加负载均衡器规则

powershell - 如何使用哈希表将参数传递给在参数前使用双破折号的 powershell cmd

Azure Vault 和 VM 磁盘加密 'Access Denied'

azure - 使用部署槽,我混淆了连接字符串,如何才能让它工作?

azure - 如何将现有的 Azure Function v2.0 beta 升级到 v2.0 GA?

c# - 在运行 select 之前检查容器/集合是否存在

c# - 使用 Azure WebJobs/Azure Functions 在服务队列上安排消息?

unix-timestamp - 使用 unix-timestamp 从数据工厂过滤 DocumentDb 集合

azure - 找不到不超过 10GB 的分区键