Jamie
Jamie

Reputation: 71

ms-rest-nodeauth (azure-sdk-for-js) : Error: credentials argument needs to implement signRequest method

Trying to follow the samples from https://github.com/Azure/ms-rest-nodeauth

When passing authresponse to a client to generate a client to ping resources, I end up getting:

Error: credentials argument needs to implement signRequest method

I am trying to read through the documents to see if I need to sign the token's I am getting back from the SDK/Azure AD, but the documentation for the new SDK doesnt show anything

Upvotes: 2

Views: 856

Answers (2)

DFBerry
DFBerry

Reputation: 2008

Adding the code, using @azure/arm-billing, in case the full code file is helpful.

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

Upvotes: 1

Jamie
Jamie

Reputation: 71

Figured it out, have to call .credentials on the authresponse

Upvotes: 3

Related Questions