azure - 使用从azure存储服务获取Blob时如何正确获取授权 header ?

标签 azure powershell azure-blob-storage azure-automation

我通过使用 https://storage.azure.com 的资源在 header 中使用授权承载来存储服务.

这就是我制作标题的方式。

$Body2 = @{
    resource = "https://storage.azure.com/"
    client_id = $clientid
    grant_type = "client_credentials"
    client_secret = $password
}
$token2=Invoke-RestMethod -Uri "https://login.windows.net/$TenantId/oauth2/token" -Method 'POST' -Body $Body2 -Headers @{"accept"="application/json"} -UseBasicParsing

上面的命令给了我一个访问 token ,我将其放入标题中,如下所示。

Name                          Value                                                                                    
----                           -----                                                                                    
Authorization                  Bearer xxxxxxxxxxxxxxxxxxxxxxxxx
x-ms-version                   2017-11-09                                                                               
x-ms-date                      Fri, 10 Aug 2018 13:27:34 GMT  

我使用了这个命令,

Invoke-RestMethod -Uri "https://myaccount.blob.core.windows.net/mycontainer/myfile.txt" -Method 'GET' -Header $Header -UseBasicParsing

问题是它出现了错误 Invoke-RestMethod : AuthorizationFailureThis request is notauthorized toexecute this operation. 这很奇怪,因为我们已经授予服务主体下载的权限blob 存储。

我的 header 使用 Bearer 的方式是错误的吗?使用它有什么问题以及如何解决这个问题?

最佳答案

Is my header using Bearer is the wrong way?

据我测试,带有您生成的 token 的 header 是正确的。

What's wrong with using it and how to fix this issue?

根据您提供的错误消息,似乎是403禁止错误。并且您已授予应用程序下载 blob 的权限,那么您需要添加注册到您的存储帐户的应用程序以授予权限。您可以引用以下步骤来实现。

1.向在Azure AD中注册的应用程序授予权限。 enter image description here 2.在存储帐户中点击访问控制(IAM)并为应用程序添加权限。 enter image description here 3.工作输出。 enter image description here

关于azure - 使用从azure存储服务获取Blob时如何正确获取授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787944/

相关文章:

Azure 数据工厂存储事件触发器文件路径

c# - Azure 发布错误 - 无法复制文件 obj\Debug\build.force

powershell - 管道数字数组的 Foreach 未正确制动

powershell -> $null 到底做了什么?

c# - 带进度条的 Azure Blob 存储异步下载

iis - 是否可以在 Azure Web 角色上重新启动 IIS,而无需重新启动进程?

azure - Azure Synapse 中 Delta Lake 和 Lake 数据库之间的区别

azure - Defender 365 REST API(您没有访问资源所需的任何应用程序权限(Incident.ReadWrite.All、Incident.Read.All))

azure - 在 Web/辅助角色的 Windows Azure 实例中安装虚拟驱动器 (vhd)

c# - Azure.Storage.Blob 中是否还有另一个与 MaximumExecutionTime 和 DisableMD5Validation 等效的属性