2

尝试遵循来自https://github.com/Azure/ms-rest-nodeauth的示例

当将 authresponse 传递给客户端以生成客户端以 ping 资源时,我最终得到:

错误:凭据参数需要实现signRequest方法

我正在尝试通读文档以查看是否需要签署从 SDK/Azure AD 取回的令牌,但新 SDK 的文档没有显示任何内容

4

2 回答 2

3

想通了,必须在 authresponse 上调用 .credentials

于 2019-07-02T16:57:05.183 回答
1

添加代码,使用@azure/arm-billing,以防完整的代码文件有帮助。

// auth.json
// Create auth file with Azure CLI:`az ad sp create-for-rbac --sdk-auth > auth.json`
{
"clientId": "",
"clientSecret": "",
"subscriptionId": "",
"tenantId": "",
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
"galleryEndpointUrl": "https://gallery.azure.com/",
"managementEndpointUrl": "https://management.core.windows.net/"
}

// index.js
const msRest = require("@azure/ms-rest-js");
const msRestAzure = require("@azure/ms-rest-azure-js");
const msRestNodeAuth = require("@azure/ms-rest-nodeauth");
const armBilling = require("@azure/arm-billing");
const path = require('path');

// list billing information
const lists = async (client) => {

    try {
        let lists = [];

        const enrollmentAccounts = await client.enrollmentAccounts.list();
        lists.push(enrollmentAccounts);

        const billingPeriods = await client.billingPeriods.list();
        lists.push(billingPeriods);

        const invoices = await client.invoices.list();
        lists.push(invoices);

        return lists;
    } catch (err) {
        console.log(err);
        throw (err);
    }

}

// sample auth file created from Azure CLI - removed PII
const authenticationFile = path.join(__dirname, "./auth.json");
const options = {
    filePath: authenticationFile
};

// get subscriptionId from auth file
const subscriptionIdFromAuthFile = require('./auth.json').subscriptionId;

// authenticate and getting billing information
msRestNodeAuth.loginWithAuthFileWithAuthResponse(options).then(async (response) => {
    
    console.log("authenticated");
    
    // --- CHANGE response parameter to -> response.credentials
    const client = new armBilling.BillingManagementClient(response.credentials, subscriptionIdFromAuthFile);
    console.log("client created");
    
    const results = await lists(client);
    console.log(`The result is:${JSON.stringify(results)}`);

}).catch((err) => {
    console.error(err);
}); 
于 2020-09-27T20:34:08.210 回答