如何使用REST Web Api for VSTS阅读用户权利

时间:2018-06-19 15:53:01

标签: rest tfs azure-devops

如何使用REST Web Api for VSTS阅读用户权利?

目前我的代码是这样,但没有得到结果。

 var accountUriString = "https://app.vssps.visualstudio.com/";
                Uri accountUri = new Uri(accountUriString);

                LicensingHttpClient licensingHttpClient = new LicensingHttpClient(accountUri, vssCredentials);
                Task<AccountEntitlement> accountEntitlementAsync = licensingHttpClient.GetAccountEntitlementAsync();
                var accountEntitlement = accountEntitlementAsync.Result;
                var license = accountEntitlement.License;

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

String collectionUri = "https://{account}.visualstudio.com";
VssCredentials creds = new VssClientCredentials();
creds.Storage = new VssClientCredentialStorage();
VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
var licensingHttpClient = connection.GetClient<LicensingHttpClient>();
var accountEntitlement = licensingHttpClient.GetAccountEntitlementAsync().Result;
var license = accountEntitlement.License;

答案 1 :(得分:0)

我已经完成了上面建议的操作,但是仍然遇到VssCredentials问题。

我已使用通过AccessToken验证的用户A登录到VSTS。当我使用accesstoken创建凭据时,出现以下错误:

error with vsscredentials

VssCredentials credentialsWithToken = new VssCredentials(new VssOAuthCredential(new VssOAuthToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im9PdmN6NU1fN3AtSGpJS2xGWHo5M3VfVjBabyJ9.eyJuYW1laWQiOiI5ZDhmODU0Zi02N2NkLTQ2YjktYjAwMS0xNTJkNjBlZTM5YTYiLCJzY3AiOiJ2c28uY29kZV9mdWxsIHZzby5leHRlbnNpb24uZGVmYXVsdCB2c28uaWRlbnRpdHlfbWFuYWdlIHZzby5wcm9qZWN0X21hbmFnZSB2c28ucHJveHkgdnNvLnNlY3VyaXR5X21hbmFnZSB2c28udGVzdF93cml0ZSB2c28ud29ya19mdWxsIiwiYXVpIjoiYzMxMmY5OWYtMDExYS00NmVkLWE4NGItZWY1NjIwODBiMjM3IiwiaGFpIjoiM2NlOGViZjEtNzE1Mi00NjJhLWI1NDMtZWYyOGQyNzgyMDdmIiwic2lkIjoiZmU3ZWM3YTUtOGYzMy00NDdiLWJmY2MtNWVlMzVjZjY0ZDU4IiwiaXNzIjoiYXBwLnZzc3BzLnZpc3VhbHN0dWRpby5jb20iLCJhdWQiOiJhcHAudnNzcHMudmlzdWFsc3R1ZGlvLmNvbXx2c286M2U5MTQwODUtZjIwYS00MmM3LTgzNmQtMTk0ZGU1MTU0MGMyIiwibmJmIjoxNTI5NDk3NTczLCJleHAiOjE1Mjk1MDE3NzN9.ZeP24wM_-REvgrFvKEssheaLfzQLLaeBrS809RPNcZ94jNVgRR10WDXqN1Yvk7khpMiOz6TJF4zCKiVf6utfiJZzFwIjejC6oRicDNvYlEeJj08HGeNIYRbvE1O6irKDFCrxIZTb4W6pzwSfODR7q3JXQIE9mCJA6-RednR2BoAX7pt6qUNMZgjAguRXtenF8c-7-dLOOSNqxFTdXTkV6UnWRoBiepNSAlYckYJ_ZiSXfgW98nYa8aijadI8jLLZ6MbVwjC7Q5FZvEQ_Ibnd-GGix0xJs06VP1zA2vhUq_tnkkr-GBf31RDacveCsbIX9EH5alXJeTsZghs-fSMGbQ")));
                Microsoft.VisualStudio.Services.WebApi.VssConnection connection = new Microsoft.VisualStudio.Services.WebApi.VssConnection(new Uri(this.ServerUri), credentialsWithToken);
                var licensingHttpClient = connection.GetClient<LicensingHttpClient>();
                var accountEntitlement = licensingHttpClient.GetAccountEntitlementAsync().Result;
                var license = accountEntitlement.License;

在这里我想清除我无法使用默认凭据创建凭据,我需要登录用户的许可信息。下面的代码可以与accesstoken凭据一起正常工作:

WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(new Uri(this.ServerUri), vssCredentials);
                Stream serverFileStream = workItemTrackingHttpClient.GetAttachmentContentAsync(new Guid(fileNameGuid), fileName).Result;
                result = new OperationResult(OperationStatusTypes.Success, serverFileStream);

谢谢