如何使用个人访问令牌向Visual Studio Team Services和Team Foundation Server进行身份验证?

如何使用个人访问令牌向Visual Studio Team Services和Team Foundation Server进行身份验证?

问题描述:

如何从PowerShell中使用个人访问令牌(PAT)对我的Visual Studio Team Services(VSTS)帐户或本地Team Foundation Server(TFS)进行身份验证?

From PowerShell, how do I use Personal Access Tokens (PAT) to authenticate to my Visual Studio Team Services (VSTS) account or on-premises Team Foundation Server (TFS)?

As of July 2015, Visual Studio Online allows users to create Personal Access Tokens (PAT) as a more secure option than alternate credentials.

要通过 REST API 进行身份验证,只需使用PAT作为基本身份验证 HTTP标头中的密码部分以及您的REST请求.

To authenticate to the REST APIs, all you need to do is use the PAT as the password portion in a Basic Auth HTTP Header along with your REST request.

$personalAccessToken = "your-personal-access-token-here"
$uri = "https://your-account.visualstudio.com/DefaultCollection/_apis/wit/workitems?api-version=1.0&ids=1,2,3,4"

Invoke-RestMethod `
-Uri $uri `
-Headers @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) } 

请注意,当您使用个人访问令牌时,基本身份验证标头的用户名部分将被完全忽略.您可以改用("BLAHBLAH:$($personalAccessToken)")),它仍然可以正常工作.

Note, that the username portion of the Basic Auth header is completely ignored when you use a personal access token. You could have ("BLAHBLAH:$($personalAccessToken)")) instead and it will still work fine.