none
Get Access token from an Outlook Addin using ADAL.js RRS feed

  • Question

  • Hi!

    I need to get an access token (that gets refreshed using the refresh token). I tried to use MSAL.js but it did not work... I was in contact with the MSAL.js support and they concluded that due to the iframe issue in office addins it simply does not yet work.

    So I am trying my luck with ADAL.js.

    What I do is to do this in the home.js:

        // The initialize function must be run each time a new page is loaded
        Office.initialize = function (reason) {
            $(document).ready(function () {
                app.initialize();
                _mailbox = Office.context.mailbox;
                _mailbox.getUserIdentityTokenAsync(getUserIdentityTokenCallback);
                setEmailAddress(_mailbox.userProfile.emailAddress);
            });
        };
    

    then in the Home.html I have this:

            //For ADAL.js
            window.config = {
                tenant: '<<<my tenant>>',
                clientId: '<<my client id>>',
                postLogoutRedirectUri: window.location.origin,
                endpoints: {
                    officeGraph: 'https://graph.microsoft.com'
                },
                cacheLocation: 'localStorage'
            };

    And the setEmailAddress is the following:

            function setEmailAddress(p) {
                console.log("Setting this email address: " + p);
                g_szEmailAddress = p;
    
                authenticationContext = new AuthenticationContext(config);
                var isCallback = authenticationContext.isCallback(window.location.hash);
                console.log("isCallback=" + isCallback);
                console.log("Before handleWindowCallback");
                authenticationContext.handleWindowCallback();
                console.log("After handleWindowCallback");
                if (isCallback && !authenticationContext.getLoginError()) {
                    console.log("Before window.location to " + authenticationContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
                    window.location = authenticationContext._getItem(authenticationContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
                    console.log("After window.location");
                }
                var user = authenticationContext.getCachedUser();
                console.log("After authenticationContext.getCachedUser");
                if (user) {
                    console.log("GetCachedUser returned user " + user);
                    authenticationContext.acquireToken(config.endpoints.officeGraph, function (error, token) {
                        if (error || !token) {
                            console.log("No token: " + error);
                        } else console.log("SUCCESS getting token: " + token);
                    });
                } else {
                    authenticationContext.login();
                }
            }
    

    When debugging using Chrome, when the addin is loaded I see:

    

    Is there any way to make ADAL.js work to get the access token? All I need to do is to upload a file to OneDrive.

    Storing the tokens in a server (that is, to get the access token from a refresh token dictionary stored in a server) is not a choice since we cannot store tokens in a server because of security reasons.

    Thanks.

    Wednesday, March 11, 2020 3:28 PM