How do I authenticate to Visual Studio Team Services and Team Foundation Server with a Personal Access Token?

后端 未结 1 860
谎友^
谎友^ 2021-02-10 17:18

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)?

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-10 17:58

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

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

    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.

    0 讨论(0)
提交回复
热议问题