【问题标题】:Create GitHub service connection using azure rest api giving error使用 azure rest api 创建 GitHub 服务连接给出错误
【发布时间】:2020-12-22 10:38:03
【问题描述】:

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

在邮递员中我把下面的 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 服务连接?

【问题讨论】:

    标签: github azure-devops azure-devops-rest-api


    【解决方案1】:

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

    您是否正在创建此 GitHub 服务连接?

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

    {
        "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 的值,以使主体工作。我们可以通过Projects-List API获取YourProjectID

    在 PostMan 中运行后,我可以成功验证它而不会出现问题。我猜您的问题可能与您的参数有关。使用

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

    代替

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

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

    不喜欢 Token、Name 和 Description 等其他元素,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来完成这个过程。)

    【讨论】:

    • 如何启用安全选项 - 授予对上图中显示的 json 正文中所有管道选项的访问权限?
    • @megha 此选项由另一个 API 管理,您可以查看我在回答中的更新以获取更多详细信息。
    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多