通过对话框从 Office 插件向 Office 365 进行身份验证

Authenticating to Office 365 from Office Addin via dialog

我已经为 Word 创建了一个 Office 插件。当我尝试向 Office 365 进行身份验证时,我正在使用 Office.context.ui.displayDialogAsync 打开一个对话框。我的回调成功,对话框打开。对话框打开后,api 调用 _dlg.addEventHandler(Office.EventType.DialogEventReceived, processMessage);,返回错误代码 12003,这意味着需要 https,但我的页面是通过 https 提供的。

如果我的页面是通过 https 提供的,我不确定为什么会收到此错误?

$scope.startLogin = function () {
                showLoginPopup("/Auth.html").then(function successCallback(response) {

                    // authentication has succeeded but to get the authenication context for the
                    // user which is stored in localStorage we need to reload the page.
                    window.location.reload();
                }, function errorCallback(response) {
                    console.log(response);
                });
            }; 



var _dlg;
var _dlgDefer;  

var showLoginPopup = function (url) {
                _dlgDefer = $q.defer();

                Office.context.ui.displayDialogAsync('https://' + location.hostname + url, { height: 40, width: 40}, function (result) {
                    console.log("dialog has initialized. wiring up events");
                    _dlg = result.value;
                    console.log(result.value)
                    _dlg.addEventHandler(Office.EventType.DialogEventReceived, processMessage);
                });

                return _dlgDefer.promise;
            } 

function processMessage(arg) {
                console.log(arg.error)
                var msg = arg.message;
                console.log("Message received in processMessage");
                if (msg && msg === "success") {
                    //we now have a valid auth token in the localStorage
                    _dlg.close();
                    _dlgDefer.resolve();
                } else {
                    //something went wrong with authentication
                    _dlg.close();
                    console.log("Authentication failed: " + arg.message);
                    _dlgDefer.reject();
                }
            }

您代码中的某些内容让我认为您可能使用了旧示例。 Dialog API 发生了一些变化。如果您还没有,请阅读 Use the Dialog API in your Office Add-ins

12003 并不意味着作为参数传递给 displayDialogAsync() 的原始页面是非 HTTPS 的。相反,这意味着对话框在最初打开 HTTPS URL 后已被重定向到非 HTTPS 地址。您需要查看 Auth.html 页面的脚本并查看 URL 对话框被重定向到什么。

我注意到的另一件事: 您的 DialogEventReceived 处理程序将 "success" 测试为 arg.message。 DialogEventReceived 现在实际上仅用于错误处理。通常,应该通过调用 messageParent() 将成功传送到主机页面。您使用事件处理程序处理主机页面上的这些消息 DialogMessageReceived.