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

Wal*_*shi 2 azure-devops azure-devops-rest-api

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

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.

        });

    });
});
Run Code Online (Sandbox Code Playgroud)

R P*_*zer 5

查看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);
}
Run Code Online (Sandbox Code Playgroud)

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


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

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