2017-07-07 100 views
0

我正尝试访问我的扩展代码中的服务端点设置。从javascript访问VSTS服务端点

扩展如下:

{ 
    "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。

任何想法?

感谢 马丁

+0

,你看到了什么错误? –

回答

1

基础上supported scopes,没有对服务端点的范围,所以你不能做到这一点。

我在这里提交用户的声音:VSTS extension service endpoint scope,你可以投票和跟进。

的解决方法是,你可以通过使用JS代码在你的扩展Personal Access Token调用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); 
     });