Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Work with Customer Insights APIs Token

(1) ShareShare
ReportReport
Posted on by 45

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
    DGA205 45 on at
    RE: Work with Customer Insights APIs Token

    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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,111 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,934 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans