使用 Get Blob from azure storage service 时如何正确获取授权header?
How to get authorization header correctly when using Get Blob from azure storage service?
我在我的 header 中使用 Authorization Bearer 通过使用 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
上面的命令给了我一个访问令牌,我把它放在我的 header 中,如下所示。
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 not authorized to perform this operation.
错误,这很奇怪,因为我们已经授予服务主体从 blob 存储下载的权限。
是不是我的header使用Bearer的方式不对?使用它有什么问题以及如何解决这个问题?
Is my header using Bearer is the wrong way?
经我测试,header你生成的token是对的
What's wrong with using it and how to fix this issue?
根据您提供的错误信息,似乎是403 forbidden
错误。并且你已经授予应用程序下载blob的权限,那么你需要添加注册到你的存储帐户的应用程序来授予权限。您可以参考以下步骤来实现。
1.Grant 对在 Azure AD 中注册的 App 的权限。
2.In 存储帐户单击 Access control(IAM)
并为应用程序添加权限。
3.The 工作输出。
我在我的 header 中使用 Authorization Bearer 通过使用 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
上面的命令给了我一个访问令牌,我把它放在我的 header 中,如下所示。
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 not authorized to perform this operation.
错误,这很奇怪,因为我们已经授予服务主体从 blob 存储下载的权限。
是不是我的header使用Bearer的方式不对?使用它有什么问题以及如何解决这个问题?
Is my header using Bearer is the wrong way?
经我测试,header你生成的token是对的
What's wrong with using it and how to fix this issue?
根据您提供的错误信息,似乎是403 forbidden
错误。并且你已经授予应用程序下载blob的权限,那么你需要添加注册到你的存储帐户的应用程序来授予权限。您可以参考以下步骤来实现。
1.Grant 对在 Azure AD 中注册的 App 的权限。
Access control(IAM)
并为应用程序添加权限。