Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VssClientCredentials with accesstoken credentials in VSTS authentication

I am reading licensing information of user from VSTS but it is not authenticating itself with Access Token credentials.

How do you authenticate with an access token?

string accessToken = $"{AccessTokenHere}";
VssOAuthAccessTokenCredential accessTokenCredentials = new VssOAuthAccessTokenCredential(new VssOAuthAccessToken(accessToken));
var credentials = new VssClientCredentials(accessTokenCredentials);

VssConnection connection = new VssConnection(new Uri(this.ServerUri), credentials);
var licensingHttpClient = connection.GetClient<LicensingHttpClient>();
var accountEntitlement = licensingHttpClient.GetAccountEntitlementAsync().Result;
var license = accountEntitlement.License;
like image 884
arsalan younus Avatar asked Sep 16 '25 12:09

arsalan younus


2 Answers

I think, you can skip the following line, when you have the bearer token from an oauth2 authentication:

// skip this line in your code: var credentials = new VssClientCredentials(accessTokenCredentials);

For me, this code is working:

VssOAuthAccessTokenCredential credentials = new VssOAuthAccessTokenCredential(AccessToken);
VssConnection connection = new VssConnection(new Uri("https://dev.azure.com/[your org]"), credentials);
var client = connection.GetClient<ProjectHttpClient>();
var projects = client.GetProjects().Result;

Please also make sure, you register the correct scopes in your application for whatever you are trying to achieve.

like image 193
AIsmaili Avatar answered Sep 18 '25 08:09

AIsmaili


Try the code below:

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

About personal access token, you can refer to the link below:

https://learn.microsoft.com/en-us/vsts/accounts/use-personal-access-tokens-to-authenticate?view=vsts

like image 31
Cece Dong - MSFT Avatar answered Sep 18 '25 08:09

Cece Dong - MSFT