Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

Reading data from another CRM instance using webapi in a javascript.

Posted on by 35

Hi,

We are having one requirement where we want to read data from another crm instance using a web api in a javascript.

while doing this I got an authentication issue and to resolve this I need to set a reqest header for an authentication and pass an access token.

And to get this token I used MSAL library but it is giving me a sign in page, I want to get an access token silently i.e without a log in pop.

How can I achive this without login popup,can anyone please guide me in the right  direction.

please find my code below :

 getAccessToken: function () {
        const msalConfig = {
            auth: {
                clientId: "XXXXX-XX-XXX-XXXXXX",
                authority: "https://login.microsoftonline.com/XXXXX-XX-XXX-XXXXXX",
                redirectUri: "https://XXXX.crm4.dynamics.com",
            },
            cache: {
                cacheLocation: "sessionStorage", // This configures where your cache will be stored
                storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
            },
            system: {
                loggerOptions: {
                    loggerCallback: (level, message, containsPii) => {
                        if (containsPii) {
                            return;
                        }
                        switch (level) {
                            case msal.LogLevel.Error:
                                console.error(message);
                                return;
                            case msal.LogLevel.Info:
                                console.info(message);
                                return;
                            case msal.LogLevel.Verbose:
                                console.debug(message);
                                return;
                            case msal.LogLevel.Warning:
                                console.warn(message);
                                return;
                        }
                    }
                }
            }
        };

        var request = {
            scopes: ["User.Read", "Mail.Read"],
            forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
        };
        var userAgentApplication = new Msal.UserAgentApplication(msalConfig);

        userAgentApplication.loginPopup(request.scopes).then(function (idToken) {
            userAgentApplication.acquireTokenSilent(request.scopes).then(function (accessToken) {
                //AcquireToken Success
                alert("token 1 "   accessToken);
            },
                function (error) {
                    //AcquireToken Failure, send an interactive request.
                    userAgentApplication.acquireTokenPopup(request.scopes).then(function (accessToken) {
                        alert("token 2 "   accessToken);

                    },
                        function (error) {
                            console.log("error child :: " error);
                        });
                });
        }, function (error) {
            console.log("error main :: "   error);
        });
    },

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans