【问题标题】:Authentication Failed while using Azure AD Bearer Token, to return list of containers [Azure Blob] [Azure AD OAuth 2.0] [REST API]使用 Azure AD 承载令牌时身份验证失败,返回容器列表 [Azure Blob] [Azure AD OAuth 2.0] [REST API]
【发布时间】: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


【解决方案1】:

尝试将scope 更改为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) -> 添加角色分配以将服务主体添加到您的存储帐户

代码:

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);
  });

【讨论】:

  • 我对“将服务主体添加到您的帐户”的确切含义有点困惑我已经用 Azure 门户访问控制 (IAM) 的 ss 更新了上述问题,看看并让我知道我错过了什么。
  • 嗨,@MayankPatel。我在回复中添加了屏幕截图。 pamelaTestApplication 是用于身份验证的应用程序。
  • 我添加了 Mayank Test Registration App 作为 Storage Blob Data Contributor,同一个 app 用于生成 ClientID、ClientSecret、TenantID,但还是一样的 Auth Failed 错误。
  • 对不起,范围应该是https://${account}.blob.core.windows.net/.default。我将它与其他流程混淆了:(您可以发布访问令牌,我已经在我的答案中分享了代码。我指的是document
  • 它解决了@Pamela 的问题,我接受了你的回答。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 2017-05-19
相关资源
最近更新 更多