gpt4 book ai didi

node.js - 使用 Azure AD 承载 token 时身份验证失败,无法返回容器列表 [Azure Blob] [Azure AD OAuth 2.0] [REST API]

转载 作者:行者123 更新时间:2023-12-03 05:29:19 27 4
gpt4 key购买 nike

我已成功尝试使用 Shared key 执行身份验证然后制作 REST calls Azure Blob 。现在我尝试使用 AzureAD OAuth 2.0 进行身份验证,接收不记名 token 并将其传递给 Authentication 使 REST calls 。我成功获得Bearer token但无法进行身份验证。

代码如下:

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

const account = process.env.ACCOUNT_NAME || "";
const key = process.env.ACCOUNT_KEY || "";
const tenantId = process.env.AZURE_TENANT_ID || "";
const clientId = process.env.AZURE_CLIENT_ID || "";
const clientSecret = process.env.AZURE_CLIENT_SECRET || "";

const options = {
url: `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
formData: {
grant_type: "client_credentials",
client_id: clientId,
scope: "https://graph.microsoft.com/.default",
// scope:"http://storage.azure.com/.default",
client_secret: clientSecret,
},
headers: {
"Content-Type": `application/x-www-form-urlencoded`,
},
};

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

function callback(error, response, body) {
const options = {
url: `https://${account}.blob.core.windows.net/?comp=list`,

headers: {
Authorization: `Bearer ${JSON.parse(response.body).access_token}`,
"x-ms-date": strTime,
"x-ms-version": "2019-02-02",
},
};

request(options, function (error, response, body) {
console.log("Response is: ", response.statusCode, response.statusMessage);
});
}

request(options, callback);

当我尝试运行它时,它显示身份验证失败。

 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

ScreenShot of Output

以下是一些引用链接: Service-Service calls using client credentials , OAuth 2.0 client credentials flow

编辑:尝试了两个链接的范围,选项 url 从 https://login.microsoftonline.com/${tenantId}/oauth2/token 更新。至https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token

访问控制的屏幕截图。 SS of Access Control(IAM)

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

最佳答案

尝试使用 https://${account}.blob.core.windows.net/.defaulthttps://更改范围 storage.azure.com/.default.

注释:

  1. 范围在“v2.0”中受支持。如果使用 v1.0,需要将 scope 替换为 resource,代码如下 resource: "https://${account}.blob.core .windows.net/".

  2. 使用formData时,必须设置“multipart/form-data”。

  3. 导航到 Azure 存储 -> 访问控制 (IAM) -> 添加角色分配以将服务主体添加到您的存储帐户

enter image description here

代码:

const request = require("request");
require("dotenv").config();
const axios = require('axios');
const qs = require('qs');

const account = "";
const key = "";
const tenantId = "";
const clientId = "";
const clientSecret = "";

const postData = {
client_id: clientId,
scope: `https://${account}.blob.core.windows.net/.default`,
client_secret: clientSecret,
grant_type: 'client_credentials'
};

axios.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';

let token = '';

axios.post(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, qs.stringify(postData))
.then(response => {
console.log(response.data);
token = response.data.access_token;
})
.catch(error => {
console.log(error);
});

关于node.js - 使用 Azure AD 承载 token 时身份验证失败,无法返回容器列表 [Azure Blob] [Azure AD OAuth 2.0] [REST API],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66487683/

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