创建 GitHub 服务连接使用 azure rest api 给出错误

Create GitHub service connection using azure rest api giving error

我正在尝试使用 azure Devops rest 创建 GitHub 服务连接 api -

在邮递员中我放了以下Json Body-

{
   "name": "release-1",
   "type": "github",
   "url": "https://github.com",
   "authorization": {
   "scheme": "PersonalAccessToken",
   "parameters": {
   "accessToken": "<Github_Personal_Access_Token>"
  }
 }
}

这会创建服务连接,但是当我在 UI 中打开服务连接并尝试验证时它会出错,但是如果我在 UI 中编辑并替换为相同的 Github 令牌,那么它作品。似乎它没有接受我在 Json 正文中提供的令牌 -

我在这里也看到了关于这个持续问题的信息 - https://ljvmiranda921.github.io/notebook/2019/12/28/workaround-azure-github-pat/

我想自动创建 GitHub 服务连接。

如何使用 Azure Devops rest api 创建 GitHub 服务连接?

How to create GitHub service connection using Azure Devops rest api ?

您要创建此 GitHub 服务连接吗?

如果是这样,您应该使用 EndPoints-Create API 并且您的 Json 正文可以遵循以下格式:

{
    "authorization": {
        "scheme": "Token",
        "parameters": {"AccessToken": "YourGitHubPAT"}
    },
    "data": {},
    "description": "",
    "name": "YourServiceConnectionName",
    "serviceEndpointProjectReferences": [{
        "description": "",
        "name": "YourServiceConnectionName",
        "projectReference": {
            "id": "YourProjectID",
            "name": "YourProjectName"
        }
    }],
    "type": "github",
    "url": "https://github.com",
    "isShared": false
}

我们应该把YourGitHubPATYourServiceConnectionName(两点两次)、YourProjectIDYourProjectName的值输入自己的值,才能让body工作。我们可以通过 Projects-List API.

得到 YourProjectID

在 PostMan 中 运行 之后,我可以毫无问题地成功验证它。我想你的问题可能与你的参数有关。使用

"authorization": {
    "scheme": "Token",
    "parameters": {"AccessToken": "<Github_Personal_Access_Token>"}
},

而不是

"authorization": {
   "scheme": "PersonalAccessToken",
   "parameters": {"accessToken": "<Github_Personal_Access_Token>"}

有关如何启用对所有管道的授权访问的更新:

不喜欢令牌、名称和描述等其他元素,Grant Access Permissions to all pipelines 选项由另一个 API 管理。

API 启用此选项:

https://dev.azure.com/{YourOrganizationName}/{YourProjectName}/_apis/pipelines/pipelinePermissions/endpoint/{YourEndPointID}?api-version=5.1-preview.1

正文:

{
    "resource": {
        "id": "YourEndPointID",
        "type": "endpoint",
        "name": ""
    },
    "pipelines": [],
    "allPipelines": {
        "authorized": true,
        "authorizedBy": null,
        "authorizedOn": null
    }
}

注意:我们需要在URL和Body中输入EnterPointID。 (两次!)

在 PostMan 中,您应该使用 PATCH 方法和 application/json 类型。 YourEndPointIDServiceConnectionID,您可以从 EndPoints-Create API 的响应中获取此值。

所以通常要在启用“授予所有人访问权限...”的情况下创建服务连接,我们应该先 运行 Endpoints-Create 然后 运行 第二个 API启用此选项。

(这与Web Portal中的行为相同。当我们在Web浏览器中单击Verify and Save按钮时,它实际上也调用了这两个API来完成该过程。)