【发布时间】:2021-06-03 20:19:51
【问题描述】:
我已成功尝试使用Shared key 执行身份验证,然后将REST calls 设置为Azure Blob。
现在我正在尝试使用 AzureAD OAuth 2.0 进行身份验证,以接收承载令牌并将其传递给 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);
当我尝试运行它时显示 Auth failed。
403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
以下是一些参考链接: 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
但是,同样的错误仍然存在。
【问题讨论】:
-
你看到了吗:docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad?您的服务主体需要具有其他角色才能访问数据平面 API(例如列出容器)+ 范围也会有所不同。
-
你试过使用'url:
https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token,'吗? v2.0 在这里。 -
您提供的链接使用的是v2.0版本,您的代码没有使用过。
标签: node.js azure azure-active-directory azure-blob-storage rest