Javascript 在邮递员预请求脚本中获取Azure REST访问令牌

Javascript 在邮递员预请求脚本中获取Azure REST访问令牌,javascript,azure,postman,azure-managed-identity,azure-node-sdk,Javascript,Azure,Postman,Azure Managed Identity,Azure Node Sdk,有人知道在Postman预请求脚本中获取Azure访问令牌的最佳方法是什么吗?尝试获取当前登录用户的令牌,而不必创建服务主体,如中所述 我尝试了预请求脚本: var msRestAzure = require('ms-rest-azure'); function getAccessToken(){ return msRestAzure.loginWithAppServiceMSI({resource: 'https://management.azure.com/'}); } pm.glo

有人知道在Postman预请求脚本中获取Azure访问令牌的最佳方法是什么吗?尝试获取当前登录用户的令牌,而不必创建服务主体,如中所述

我尝试了预请求脚本:

var msRestAzure = require('ms-rest-azure');
function getAccessToken(){
    return msRestAzure.loginWithAppServiceMSI({resource: 'https://management.azure.com/'});
}
pm.globals.set("access_token", getAccessToken());
但它不断抛出错误:
评估预请求脚本时出错:错误:找不到模块“ms rest azure”
。截图如下:
需要在应用程序服务中使用
loginWithAppServiceMSI
,它将使用应用程序服务的获取令牌,在邮递员预请求脚本中,它不支持使用令牌


我的访问受到限制,无法创建具有所需访问权限的服务主体。要使用我的凭据进行本地测试

在这种情况下,如果要使用用户凭据在预请求脚本中获取令牌,则可以选择使用

注意:

  • 由于安全问题,不建议使用ROPC流,您需要在邮递员中公开用户名和密码,如果您的用户帐户已启用MFA,则它将不起作用

  • 要使用此流程,您还需要一个广告应用程序(应用程序注册),如果您没有创建广告应用程序的权限,解决方法是使用Microsoft内置应用程序,例如Microsoft Azure PowerShell,您可以使用此方法进行测试,但我不建议您在生产环境中使用它


  • 请按照以下步骤操作:

    1.更改postman集合中的预请求脚本,如下所示

    pm.sendRequest({
        url: 'https://login.microsoftonline.com/' + pm.variables.get("tenantId") + '/oauth2/token',
        method: 'POST',
        header: 'Content-Type: application/x-www-form-urlencoded',
        body: {
            mode: 'urlencoded',
            urlencoded: [ 
                {key: "grant_type", value: "password", disabled: false},
                {key: "client_id", value: pm.variables.get("clientId"), disabled: false},
                {key: "username", value: pm.variables.get("username"), disabled: false},
                {key: "resource", value: pm.variables.get("resource"), disabled: false},
                {key: "password", value: pm.variables.get("password"), disabled: false}
            ]
        }
    }, function (err, res) {
        pm.globals.set("bearerToken", res.json().access_token);
    });
    
    clientId
    resource
    subscriptionId
    tenantId
    username
    password
    
    2.使用如下变量

    pm.sendRequest({
        url: 'https://login.microsoftonline.com/' + pm.variables.get("tenantId") + '/oauth2/token',
        method: 'POST',
        header: 'Content-Type: application/x-www-form-urlencoded',
        body: {
            mode: 'urlencoded',
            urlencoded: [ 
                {key: "grant_type", value: "password", disabled: false},
                {key: "client_id", value: pm.variables.get("clientId"), disabled: false},
                {key: "username", value: pm.variables.get("username"), disabled: false},
                {key: "resource", value: pm.variables.get("resource"), disabled: false},
                {key: "password", value: pm.variables.get("password"), disabled: false}
            ]
        }
    }, function (err, res) {
        pm.globals.set("bearerToken", res.json().access_token);
    });
    
    clientId
    resource
    subscriptionId
    tenantId
    username
    password
    
    注意
    客户端ID
    1950a258-227b-4e31-a9cf-717495945fc2
    ,它是Microsoft应用程序
    Microsoft Azure PowerShell
    客户端ID,请勿更改

    3.其他设置与您提供的相同,然后发送请求以获取资源组,在我这边可以正常工作


    这能解决您的问题吗?为什么不能使用博客中提到的脚本?我的访问权限受到限制,无法创建具有所需访问权限的服务主体。希望使用我的凭据进行本地测试。感谢您的回复@Joy这是有用的信息。但不幸的是没有帮助,因为我已经打开了MFA。在C#控制台应用程序上,我们可以执行
    var token=new AzureServiceTokenProvider().GetAccessTokenAsync(“https://management.azure.com/“”
    我们不能在JS中做类似的事情吗?仅供参考,我有一个C#控制台应用程序,它可以在我的本地机器上运行。@dushyantp 1。在这种情况下,我的解决方案是唯一的方法。如果您的帐户已启用MFA,则无法在邮递员中获取令牌,因为它不允许交互式登录。2.这是一个完全不同的问题,恐怕你需要在另一个帖子中提问。