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

Notifications

Announcements

No record found.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 96 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans