【问题标题】:connect to tfs with different user by using tfs api使用 tfs api 连接到不同用户的 tfs
【发布时间】:2021-05-21 09:20:47
【问题描述】:

我正在尝试运行一个 shellscript 程序以使用 tfsapi 服务连接 tfs。

Jenkins 在登录用户上运行,但我想将 tfs 连接到不同的用户(而不是 Windows 登录用户)

如何向我的 powershell 代码添加凭据?我使用 UseDefaultCredentials 但需要使用不同的用户来连接 tfs。我该怎么做?

$tfsServerURL = "https://test.tfs.siemens.net/test"
$BuildDefinition = "test.rgs.project"

$URL = "$($tfsServerURL)"
#Get ID of Builddefinition
$buildDefinitionID = (Invoke-RestMethod -Uri ($URL + '/_apis/build/definitions?api-version=2.0&name=' + $BuildDefinition) -Method GET -UseDefaultCredentials).value.id

【问题讨论】:

    标签: powershell api tfs


    【解决方案1】:

    我们可以使用这个官方的 Rest API:Definitions - List 来获取带有附加参数 name 的特定构建定义。这个API支持Oauth2 authentication。但是,Azure DevOps 服务器不支持 OAuth 2.0。所以我们需要使用PAT authentication,其范围最小:vso.build

    因此,如果您想使用此 API 将 tfs 连接到不同的用户,请提供他们对应的 PAT,然后下面的脚本应该可以正常工作。

    $tfsServerURL = "https://test.tfs.siemens.net/test"
    $BuildDefinition = "test.rgs.project"
    
    $URL = "$($tfsServerURL)"
    
    $connectionToken="PAT here"
    
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    
    $AzureDevOpsAuthenicationHeader = @{authorization = "Basic $base64AuthInfo"}
    
    #Get ID of Builddefinition
    $buildDefinitionID = (Invoke-RestMethod -Uri ($URL + '/_apis/build/definitions?api-version=5.0&name=' + $BuildDefinition) -Method GET -Headers $AzureDevOpsAuthenicationHeader).value[0].id
    
    Write-host $buildDefinitionID
    

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 1970-01-01
      • 2017-05-31
      • 2015-04-10
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      相关资源
      最近更新 更多