2

I am new to node.js 'azure-devops-node-api'. I want to connect with my collection, getPersonalAccessTokenHandler(token) method works fine, But I want to authenticate with username and password. getNtlmHandler(username, password) do authenticates but I am unable to get respositories with this approach. Kindly suggest me a better method to authenticate please

const azdev = require("azure-devops-node-api");

const collectionURL = 'https://dev.azure.com/username';
let authHandler = azdev.getNtlmHandler('username', 'password');
let connection = new azdev.WebApi(collectionURL, authHandler);

connection.connect().then(connData => {
    console.log(`Connection established successfully!!!. This is 
    ${connData.authenticatedUser.providerDisplayName}. Welcome!!!`);

    connection.getGitApi().then(vstsGit => {
        vstsGit.getRepositories('projectName').then(repos => {

            // repos is null or undefined
            console.log('There are', repos.length, 'repositories in this 
            project');
            // But When I authenticates with Token, It works fine.

        });

    });
});

1 Answer 1

5

When looking to the azure-devops-node-api source code, you can see that there are 4 different ways to authenticate.

export function getBasicHandler(username: string, password: string): VsoBaseInterfaces.IRequestHandler {
    return new basicm.BasicCredentialHandler(username, password);
}

export function getNtlmHandler(username: string, password: string, workstation?: string, domain?: string): VsoBaseInterfaces.IRequestHandler {
    return new ntlmm.NtlmCredentialHandler(username, password, workstation, domain);
}

export function getBearerHandler(token: string): VsoBaseInterfaces.IRequestHandler {
    return new bearm.BearerCredentialHandler(token);
}

export function getPersonalAccessTokenHandler(token: string): VsoBaseInterfaces.IRequestHandler {
    return new patm.PersonalAccessTokenCredentialHandler(token);
}

Because you are only passing the Username and Password you can also use getBasicHandler() to authenticate.


Beside this, be sure that your security settings are configured in the right way. For example Alternate authentication credentials has to be toggled on in the security policies of your organisation to make use of basic authentication for the REST Api.

Azure DevOps Security reference: https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/change-application-access-policies?view=azure-devops

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.