Accessing VSTS service endpoints from javascript

戏子无情 提交于 2019-12-23 04:21:31

问题


I am trying to access the service endpoint setup in my extension code.

The extension is as follows:

{
  "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"
                }
              }
            ]
          }
        ]
      }
    }
  ],
...

The code to access the service endpoint is something like :

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')
           }
       );
   }
);

however I am not using a task and just have my endpoint in the main vss-extension.json.

Any ideas?

Thanks Martin


回答1:


Based on the supported scopes, there isn’t the scope for service endpoint, so you can’t do it.

I submit a user voice here: VSTS extension service endpoint scope, you can vote and follow up.

The workaround is that you can call REST API by using JS code with Personal Access Token in your extension.

Simple code to call 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 );
        });



回答2:


The scope has been added now and it is "vso.serviceendpoint"



来源:https://stackoverflow.com/questions/44968357/accessing-vsts-service-endpoints-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!