在 Excel 加载项中获取 Sharepoint 令牌

Get Sharepoint token in Excel add-in

我们的 Excel 加载项需要 OAUTH 令牌才能调用 SharePoint REST API 的某些功能。

加载项使用功能区命令,我们没有任务窗格。我们有以下代码来获取其他项目的token:

var dhi = dhi || {};
dhi.adal = (function (mod) {

    var settings = {
        clientId: "xxxyyyyzzzz",
        url: "https://company.sharepoint.com"
    };


    mod.getToken = function () {

        var dfd = $.Deferred();
        //fix origin for IE
        if (!window.location.origin) {
            window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
        }

        var configOptions = {
            clientId: settings.clientId,
            postLogoutRedirectUri: window.location.origin,
            cacheLocation: 'localStorage',
        }

        window.authContext = new AuthenticationContext(configOptions);

        var isCallback = authContext.isCallback(window.location.hash);
        authContext.handleWindowCallback();

        var user = authContext.getCachedUser();

        if (!user) {
            authContext.login();

        } else {

            var cachedToken = authContext.getCachedToken(user.profile.aud);
            if (!cachedToken) {
                authContext.login();
            } else {


                var url = settings.url;

                var tok = authContext.acquireToken(url, function (error, token) {
                    console.log(error);
                    if (token != null) {
                        console.log(token);
                        sharedtoken = token;
                        dfd.resolve(token);
                    } else {
                        if (cachedToken != "" && cachedToken != null && cachedToken != undefined) {
                            dfd.resolve(cachedToken);
                        } else {
                            dfd.reject("Unable to obtain token. Please contact hotline@dhigroup.com with the following error : " + error);
                        }
                    }
                });
            }
        }
        return dfd.promise();
    }
    return mod;
})(dhi.adal || {});

并且在 functions.js 文件(定义了功能区按钮的处理程序)中,我们将此称为初始化:

(function () {
    "use strict";
    $(document).ready(function () {

        dhi.adal.getToken.then(
            function (token) {
                sharedtoken = token;
                Office.initialize = function (reason) {

                    // some code here

                }
            },
            function (error) {
                app.showNotification("Problem occured", error);
            });
    });
})();

我们面临以下问题:

1) 如果令牌已经被缓存,它就可以工作。但是,当它需要调用 authContext.login() 时,它会挂起(无处无消息)。我们相信不知道在哪里显示登录对话框(在这个加载项的第一个版本中,我们使用了一个任务窗格,它在那里显示了登录页面,但是,出于多种原因,我们需要使用这些命令。

2) 我们必须在调用 Office.initialize 之前调用它,否则它不起作用(我试图将 getToken 仅用于需要它但没有成功的函数)- 不知道为什么。这有一个副作用,我们必须总是调用 getToken(),对于按钮操作,当不需要令牌时也是如此

3) 我试图创建一个对话框来获取令牌,但还有一些其他问题 - adal.js...

中的一些问题

所以,我想问一下 - 是否有任何直接的方法可以从 Office.jas

中的 Active Directory 获取 SharePoint 令牌

如果有一些示例,那将对我们有很大帮助。

没有用于获取 SharePoint 令牌的内置方法。

至于打开身份验证对话框,您应该使用 Dialog API for this. There is a sample PowerPoint Add-in that uses the Dialog API to authenticate against Graph: PowerPoint-Add-in-Microsoft-Graph-ASPNET-InsertChart。此处使用的原则和组件应该很容易转换为 Excel 和 SharePoint。

对于身份验证库,我建议看一下 microsoft-authentication-library-for-js

您还需要确保您的函数文件在页面加载时立即分配 Office.initialize。这不是对 init 函数的出站调用,它是 属性 您也在分配 init 函数。 Office 将自动调用此方法,如果您尚未分配功能,则会导致错误。