web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Work with Customer Insights APIs Token

(1) ShareShare
ReportReport
Posted on by 61

Hi. I can't found an working example in .net core, to obtain the bearer token.

I have done all the steps described in https://developer.ci.ai.dynamics.com/

  • Create de App Registration
  • The App Permision
  • Added the App as an user en CI

I have this code

 public static async Task<AuthenticationResult> GetTokenAsync(ILogger log, bool isRenew = false)
        {
            String clientId = Environment.GetEnvironmentVariable("CI-ClientId");
            String clientSecret = Environment.GetEnvironmentVariable("CI-ClientSecret");
            String tenant = Environment.GetEnvironmentVariable("AzureTenantId");
            log.LogInformation("Obteniendo token");
            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
                                                     .WithClientSecret(clientSecret)
                                                     .WithAuthority(new Uri($"">login.microsoftonline.com/.../token"))
                                                     .Build();
            string[] scopes = new string[] { "">graph.microsoft.com/.default" };
            AuthenticationResult result = null;

            result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
            if (DateTime.Now > result.ExpiresOn.AddMinutes(-15) && isRenew == false)
            {
                log.LogInformation("Token pronto a expirar, se renueva");
                result = await GetTokenAsync(log, true);
            }


            return result;
        }:

pastedimage1666218977330v4.png

The Token is returned OK, but, if I try to use it, for example in postam, I have an error:

Bearer error="invalid_token", error_description="The signature is invalid"

pastedimage1666218784055v2.png

If I use the token generated from the swagger, it works fine

pastedimage1666218871340v3.png

pastedimage1666219120020v5.png

I have notticed, that the token generated by code, is shorten tan the swagger.

  • Suggested answer
    DGA205 Profile Picture
    61 on at

    Hi. I found the problem in  the WithAuthority url, and the scope.

    This is the final code to get a working token

    String clientId = Environment.GetEnvironmentVariable("CI-ClientId");

               String clientSecret = Environment.GetEnvironmentVariable("CI-ClientSecret");

               String tenant = Environment.GetEnvironmentVariable("AzureTenantId");

               log.LogInformation("Obteniendo token");

               IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)

                                                        .WithClientSecret(clientSecret)

                                                        .WithAuthority(new Uri($"login.microsoftonline.com/{tenant}"))

                                                        .Build();

               string[] scopes = new string[] { "api.ci.ai.microsoft.com/.default" };

               AuthenticationResult result = null;

               result = await app.AcquireTokenForClient(scopes).ExecuteAsync();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Syed Aqib Raza Profile Picture

Syed Aqib Raza 97

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 89 Most Valuable Professional

#3
11manish Profile Picture

11manish 71 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans