Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js 使用Azure AD承载令牌返回容器列表[Azure Blob][Azure AD OAuth 2.0][REST API]时身份验证失败_Node.js_Azure_Azure Active Directory_Azure Blob Storage_Restapi - Fatal编程技术网

Node.js 使用Azure AD承载令牌返回容器列表[Azure Blob][Azure AD OAuth 2.0][REST API]时身份验证失败

Node.js 使用Azure AD承载令牌返回容器列表[Azure Blob][Azure AD OAuth 2.0][REST API]时身份验证失败,node.js,azure,azure-active-directory,azure-blob-storage,restapi,Node.js,Azure,Azure Active Directory,Azure Blob Storage,Restapi,我已成功尝试使用共享密钥执行身份验证,然后对Azure Blob进行REST调用。 现在我尝试使用AzureAD OAuth 2.0进行身份验证,以接收承载令牌并将其传递给Authentication进行REST调用。 我正在成功获取承载令牌,但无法执行身份验证 代码如下: const request = require("request"); require("dotenv").config(); const account = process.env

我已成功尝试使用
共享密钥执行身份验证
,然后对
Azure Blob进行
REST调用
。 现在我尝试使用
AzureAD OAuth 2.0
进行身份验证,以接收承载令牌并将其传递给
Authentication
进行
REST调用。
我正在成功获取
承载令牌
,但无法执行身份验证

代码如下:

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时,它显示Auth失败

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

以下是一些供参考的链接: ,

编辑:尝试了两个链接的作用域,选项url从
https://login.microsoftonline.com/${tenantId}/oauth2/token
to
https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token

访问控制的屏幕截图。


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

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

注意事项:

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);
  • “v2.0”中支持作用域。如果使用v1.0,
    范围
    需要替换为
    资源
    ,代码看起来像
    资源:“https://${account}.blob.core.windows.net/”

  • 使用formData时,必须设置“多部分/表单数据”

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

  • 代码:


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

    注意事项:

    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);
    
  • “v2.0”中支持作用域。如果使用v1.0,
    范围
    需要替换为
    资源
    ,代码看起来像
    资源:“https://${account}.blob.core.windows.net/”

  • 使用formData时,必须设置“多部分/表单数据”

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

  • 代码:


    你看过这个吗?您的服务主体需要具有其他角色才能访问数据平面API(例如列出容器)+范围也将不同。您是否尝试使用“url:
    https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token
    ,'?这里是v2.0。您提供的链接使用v2.0版本,而您的代码没有使用它。您看到了吗:?您的服务主体需要具有其他角色才能访问数据平面API(例如列出容器)+范围也将不同。您是否尝试使用“url:
    https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token
    ,'?这里是v2.0。您提供的链接使用的是v2.0版本,而您的代码没有使用它。我对您所说的“将服务主体添加到您的帐户”的确切含义有点困惑。我已使用Azure Portal Access Control(IAM)的ss更新了上述问题,请看一看,让我知道我缺少了什么。您好,@MayankPatel。我在回复中添加了截图。PameLateStatApplication是用于身份验证的应用程序。我已将Mayank Test Registration应用程序添加为Storage Blob Data Contributor,该应用程序用于生成ClientID、ClientSecret、TenantID,但仍然存在相同的身份验证失败错误。很抱歉,范围应为
    https://${account}.Blob.core.windows.net/.default
    。我把它和其他流程混淆了:(你可以发布访问令牌,我在我的回答中分享了代码。我指的是这个。它解决了问题@Pamela,我接受了你的回答。谢谢。我对你所说的“向你的帐户添加服务主体”的确切含义有点困惑。)我已经用Azure门户访问控制(IAM)的ss更新了上述问题,看一看,让我知道我遗漏了什么。嗨,@MayankPatel。我在回复中添加了屏幕截图。PamelatesApplication是用于身份验证的应用程序。我添加了Mayank Test Registration应用程序作为存储Blob数据贡献者,同一个应用程序用于生成ClientID、ClientSecret、TenantID,但仍然是相同的身份验证失败错误。Sorry,范围应该是
    https://${account}.blob.core.windows.net/.default
    。我把它和其他流混淆了:(你可以发布访问令牌,我在我的答案中共享了代码。我引用了这个。它解决了问题@Pamela,我接受了你的答案。谢谢