azure-devops-node-api: 使用用户名和密码进行身份验证

azure-devops-node-api: Authenticate with username and password

我是 node.js 'azure-devops-node-api' 的新手。我想连接我的 collection,getPersonalAccessTokenHandler(token) 方法工作正常,但我想使用用户名和密码进行身份验证。 getNtlmHandler(username, password) 进行身份验证,但我无法使用这种方法获取存储库。请建议我更好的身份验证方法

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.

        });

    });
});

查看 azure-devops-node-api 源代码时,您可以看到有 4 种不同的身份验证方法。

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

因为您只传递用户名和密码,所以您也可以使用 getBasicHandler() 进行身份验证。


除此之外,请确保您的安全设置配置正确。 例如,Alternate authentication credentials 必须在您组织的安全策略中 开启 才能使用 REST Api.

的基本身份验证

Azure DevOps 安全参考:https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/change-application-access-policies?view=azure-devops