gpt4 book ai didi

javascript - 尝试使用 AD token /承载 token [Azure-Blob][承载 token ] 将文件放入 Azure Blob 中时授权权限不匹配

转载 作者:行者123 更新时间:2023-12-05 04:52:25 26 4
gpt4 key购买 nike

我能够CreateContainers、ListContainers、ListBlobs,但是当我尝试发出PUT/DELETE请求来上传时>删除 Azure 存储 blob 中的文件,但发出请求后显示以下错误:

403
This request is not authorized to perform this operation using this permission.
{
'content-length': '279',
'content-type': 'application/xml',
server: 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0',
'x-ms-request-id': '4de6c154-f01e-0051-7ce4-1314ef000000',
'x-ms-version': '2018-03-28',
'x-ms-error-code': 'AuthorizationPermissionMismatch',
date: 'Mon, 08 Mar 2021 06:32:44 GMT',
connection: 'close'
}

上传/PUT文件的代码是:

const request = require("request");
require("dotenv").config();

const account = process.env.ACCOUNT_NAME || "";
const containerName = "demo";
const blobName = "dummyfile1.txt";
const blobContent = "Hello, This will be written in file";
const contentLength = new TextEncoder().encode(blobContent).length;

var strTime = new Date().toUTCString();

const options = {
url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}`,
headers: {
Authorization: "Bearer <BearerToken>",
"x-ms-date": strTime,
"x-ms-version": "2018-03-28",
"x-ms-blob-type": "BlockBlob",
"Content-Length": contentLength,
"Content-Type": 'application/text-plain',
},
body: blobContent,
};

function callback(error, response, body) {
console.log(response.statusCode);
console.log(response.statusMessage);
console.log(response.headers);
}

request.put(options, callback);

在这里,我手动替换我通过 POSTMAN 获取的内容:

POSTMAN call to get ACCESS TOKEN

此外,我已向应用程序添加了存储数据贡献者的权限:

Access Control IAM

我已将 Azure 存储、user_impersonation 权限也委托(delegate)给了该应用程序。 Azure Storage user_impersonation

但是,同样的错误仍然存​​在。

最佳答案

当使用auth code flow时,您的登录用户需要 Azure 存储的权限。使用时Storage Blob Data Contributor Angular 色,您需要将 Angular 色分配添加到您的帐户,而不是应用程序(只有客户端凭据流程需要应用程序 Angular 色)。

然后将 Azure 存储权限添加到 API 权限。

enter image description here

此外,https://<account-name>.blob.core.windows.net/user_impersonationhttps://storage.azure.com/user_impersonation可用于范围。有关 Azure 存储资源 ID(范围)的更多详细信息,请参阅 here .

https://${account}.blob.core.windows.net/.defaulthttps://storage.azure.com/.default适用于客户端凭据流程。

<小时/>

步骤:

  1. 在浏览器中获取授权码

注意:登录 Azure 帐户后,您应该接受请求的权限。

https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/authorize?
client_id={client-id}
&response_type=code
&redirect_uri=https://localhost:44300/
&response_mode=query
&scope=https://{account}.blob.core.windows.net/user_impersonation
&state=12345
&prompt=consent
  • 获取访问 token 和刷新 token 。尝试解码 https://jwt.io/ 中的访问 token ,检查aud ,看起来像 https://xxxx.blob.core.windows.net .
  • enter image description here

  • 最后,在代码中测试访问 token 。
  • enter image description here

    关于javascript - 尝试使用 AD token /承载 token [Azure-Blob][承载 token ] 将文件放入 Azure Blob 中时授权权限不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66525481/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com