我通过使用 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中注册的应用程序授予权限。
2.在存储帐户中点击访问控制(IAM)
并为应用程序添加权限。
3.工作输出。
关于azure - 使用从azure存储服务获取Blob时如何正确获取授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787944/