从javascript访问VSTS服务端点

时间:2017-07-07 10:19:40

标签: visual-studio azure-devops visual-studio-extensions

我正在尝试访问扩展程序代码中的服务端点设置。

扩展名如下:

{
  "manifestVersion": 1,
  "id": "vsts-extensions-myExtensions",
  "version": "0.5.1",
  "name": "xxx Projects Time Entry",
  "description": "Record time spent in xxx Projects",
  "publisher": "xxx",
  "targets": [
    {
      "id": "Microsoft.VisualStudio.Services"
    }
  ],
  "icons": {
    "default": "img/logo.png"
  },
  "contributions": 
  [
    {
      "id": "xxTimeEntry",
      "type": "ms.vss-dashboards-web.widget",
...
    },

    {
      "id": "service-endpoint",
      "description": "Service Endpoint type for xx connections",
      "type": "ms.vss-endpoint.service-endpoint-type",
      "targets": [ "ms.vss-endpoint.endpoint-types" ],
      "properties": {
        "name": "xxxyyy",
        "displayName": "xx server connection",
        "url": {
          "displayName": "Server Url",
          "helpText": "Url for the xxx server to connect to."
        },
        "dataSources": [
          {
            "name": "xxx Projects",
            "endpointUrl": "{{endpoint.url}}api/timesheetwidgetprojects",
            "resultSelector": "jsonpath:$[*].nm"
          }
        ],
        "authenticationSchemes": [
          {
            "type": "ms.vss-endpoint.endpoint-auth-scheme-basic",
            "inputDescriptors": [
              {
                "id": "username",
                "name": "Username",
                "description": "Username",
                "inputMode": "textbox",
                "validation": {
                  "isRequired": false,
                  "dataType": "string"
                }
              },
              {
                "id": "password",
                "name": "Password",
                "description": "Password",
                "inputMode": "passwordbox",
                "isConfidential": true,
                "validation": {
                  "isRequired": false,
                  "dataType": "string"
                }
              }
            ]
          }
        ]
      }
    }
  ],
...

访问服务端点的代码如下:

VSS.require(["VSS/Service", "VSS/WebApi/RestClient"],
   function (VSS_Service, RestClient) {
       var webContext = VSS.getWebContext();
       var client = VSS_Service.getCollectionClient(DistributedTask.TaskAgentRestClient);
       client.getServiceEndpoints(webContext.project.id).then(
           function (endpoints) {
               alert('endpoints')
           }
       );
   }
);

但是我没有使用任务,只在主vss-extension.json中使用我的端点。

有什么想法吗?

由于 马丁

2 个答案:

答案 0 :(得分:1)

基于supported scopes,没有服务端点的范围,因此您无法做到。

我在此处提交用户语音:VSTS extension service endpoint scope,您可以投票并跟进。

解决方法是,您可以在扩展程序中使用带有Personal Access Token的JS代码来调用REST API。

调用REST API的简单代码:

 $.ajax({
            url: 'https://fabrikam.visualstudio.com/defaultcollection/_apis/projects?api-version=1.0',
            dataType: 'json',
            headers: {
                'Authorization': 'Basic ' + btoa("" + ":" + myPatToken)
            }
        }).done(function( results ) {
            console.log( results.value[0].id + " " + results.value[0].name );
        });

答案 1 :(得分:1)

现在已添加范围,它是“vso.serviceendpoint”