通过 REST API 从另一个帐户将 Repo 导入到 Azure DevOps 帐户会引发 BAD 请求

Import Repo to Azure DevOps account from another account via REST APIs throws BAD request

我想将 git 存储库从另一个 Azure DevOps 帐户导入到我的新帐户。 我在 power-shell 中使用 REST API 来导入它们。 我为此使用的脚本在这里:

$TargetTokenName = 'TokenName'
$TargetPAT = 'PAT'
$base64AuthInfoTarget = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "$TargetTokenName","$TargetPAT")))
$BaseTargetUri = 'https://dev.azure.com/myOrg/myProj/_apis/'
$TargetRepoId = 'TargetRepositoryId'
$ServiceEndpointId = 'ServiceEndpointId'

$CreateImportReqRequest = '{
    "parameters" : {
        "gitSource": {
            "url" : "' + $SourceRepoUrl + '",
            }
  }
}'

$CreateImportReqUri = $BaseTargetUri + 'git/repositories/' + $TargetRepoId + '/importRequests?api-version=5.0-preview.1'
$CreateImportReqResponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $CreateImportReqUri -Body $CreateImportReqRequest -Headers @{Authorization=("Basic {0}" -f $base64AuthInfoTarget)}
echo $CreateImportReqResponse

但是调用时出现了Bad request错误。 我能够通过 REST 创建空的目标存储库和服务端点。

在下面的第一个 link 中,请求正文包含 serviceendpointid 的附加参数,但也给出了错误。

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

我参考的是: http://www.codewrecks.com/blog/index.php/2016/10/08/import-a-git-project-with-rest-api-between-vsts-team-projects/ https://docs.microsoft.com/en-us/rest/api/azure/devops/git/import%20requests/create?view=azure-devops-server-rest-5.0

错误的请求是服务端点中错误的 PAT 的结果。

此请求结构的请求现已成功:

$CreateImportReqRequest = '{
    "parameters" : {
        "gitSource": {
            "url" : "'+$SourceRepoUrl+'",
            },
            "serviceEndpointId": "' + $ServiceEndpointId + '",
            deleteServiceEndpointAfterImportIsDone : true 
  }
}'