Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用AD令牌/承载令牌[Azure Blob][承载令牌]将文件放入Azure Blob时,授权权限不匹配_Javascript_Node.js_Azure_Azure Storage Blobs_Bearer Token - Fatal编程技术网

Javascript 尝试使用AD令牌/承载令牌[Azure Blob][承载令牌]将文件放入Azure Blob时,授权权限不匹配

Javascript 尝试使用AD令牌/承载令牌[Azure Blob][承载令牌]将文件放入Azure Blob时,授权权限不匹配,javascript,node.js,azure,azure-storage-blobs,bearer-token,Javascript,Node.js,Azure,Azure Storage Blobs,Bearer Token,我可以创建容器、ListContainers、ListBlob,但当我尝试向Azure存储blob中的上传或删除文件发出放置/删除请求时,请求后会显示以下错误: 403 This request is not authorized to perform this operation using this permission. { 'content-length': '279', 'content-type': 'application/xml', server: 'Windows-

我可以创建容器、ListContainers、ListBlob,但当我尝试向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'
}
upload/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);
const request=require(“请求”);
require(“dotenv”).config();
const account=process.env.account\u NAME | |“”;
const containerName=“演示”;
const blobName=“dummyfile1.txt”;
const blobContent=“您好,这将写入文件”;
const contentLength=new textcoder().encode(blobContent).length;
var strTime=new Date().toutString();
常量选项={
url:`https://${account}.blob.core.windows.net/${containerName}/${blobName}`,
标题:{
授权:“持票人”,
“x-ms-date”:标准时间,
“x-ms-version”:“2018-03-28”,
“x-ms-blob-type”:“BlockBlob”,
“内容长度”:内容长度,
“内容类型”:“应用程序/纯文本”,
},
正文:blobContent,
};
函数回调(错误、响应、正文){
console.log(响应.状态码);
console.log(response.statusMessage);
console.log(response.headers);
}
请求.卖出(期权,回拨);
在这里,我将手动替换通过邮递员通过以下途径获得的邮件:

此外,我还向应用程序添加了Storage Data Contributor的权限:

我已将Azure存储、用户模拟权限委托给应用程序。

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

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

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

此外,这两个
https://

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


步骤:

  • 在浏览器中获取授权代码
  • 注意:当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
    
  • 获取访问令牌和刷新令牌。尝试在中解码访问令牌,检查
    aud
    ,它看起来像
    https://xxxx.blob.core.windows.net
  • 最后,在代码中测试访问令牌

  • 使用客户端凭据流。但是你在这个问题上使用。尝试添加并使用
    https://storage.azure.com/user_impersonation 脱机访问范围。@Pamela我试过使用
    https://storage.azure.com/user_impersonation offline_access
    通过邮递员访问承载令牌,并使用该承载令牌运行此请求。但是,它仍然给出了相同的错误信息。是的,同样在上一个问题中,我尝试通过客户端凭据获取承载令牌,这里我尝试通过身份验证代码流获取它。注意:我已将存储Blob数据参与者授予应用程序。您应该在门户中添加Azure存储委派权限,请参阅。我已经尝试了你的代码,它是正确的。我已经将Azure Storage delegated permission指定为user_impersonation,但仍然是相同的错误,我在问题更新中添加了屏幕截图。这很奇怪。稍后我将添加关于访问令牌的详细步骤。我能够使用该承载令牌来ListContainer、ListBlobs和CreateContainer。但是我不能创建Blob,删除Blob,GetBlob。他们需要什么特殊的角色或权限吗?嗨,@MayankPatel。您是否为您的帐户添加了
    Storage Blob Data Reader
    角色?否,我已添加
    Storage Blob Data Contributor
    作为角色。如问题中的屏幕截图所示。仅适用于应用程序,但不适用于已登录用户。尝试向用户添加
    Storage Blob Data Contributor
    ,大约10分钟后进行测试。我已经在我这边添加了角色!忘了检查:(我在没有这个角色的情况下进行了测试,它返回403。这需要太多时间才能生效。为用户帐户添加角色很重要。