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.

I have the same question (0)
  • 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 120 Super User 2026 Season 1

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 46 Most Valuable Professional

#3
NeerajPawar Profile Picture

NeerajPawar 41

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans