首页 > 解决方案 > 无法在 Outlook 加载项中从 iOS 设备执行 Microsoft OAuth

问题描述

我一直在努力解决这个问题。我已经使用 React.js 创建了一个 Outlook 插件,现在我正在尝试在这个插件中使用 Microsoft OAuth2.0。我为此目的使用了 MSAL.js,它在除 iOS 设备外的所有平台(如 Outlook for Desktop、Web 应用程序和 android 应用程序)上都运行良好。

调试代码后,我知道在执行 Auth 后调用的回调函数实际上并没有被 iOS 设备调用。这是代码

    const userAgentApp = new Msal.UserAgentApplication(config)

    const authCallback = (error, response) => {
        console.log({ error, response })
        sendRequest({ error, response })
        if (!error) {
            if (response.tokenType === "id_token") {
                localStorage.setItem("loggedIn", "yes")
            } else {
                // The tokenType is access_token, so send success message and token.
                Office.context.ui.messageParent(JSON.stringify({ status: "success", result: response.accessToken }))
            }
        } else {
            handleErrors(error, Office)
        }
    }

    userAgentApp.handleRedirectCallback(authCallback)

删除对 MSAL.js 的依赖并将我的代码更改为

Office.initialize = () => {
        if (!location.href.match(/access_token/gi)) {
            location.href = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${clientId}&response_type=token+id_token&redirect_uri=${redirectUri}&scope=openid%20profile%20email%20People.Read%20User.read&state=9Eu8.9Sz_LDcQzsZ&nonce=Hb77t324t4ugugHgUYguy2342`
        } else if (location.href.match(/access_token/gi)) {
            const response = getUrlParameters()
            if (response.hasOwnProperty('access_token')) {
                loginUser(response, Office)
            } else if (response.hasOwnProperty('error')) {
                Office.context.ui.messageParent(JSON.stringify({ status: "failure", result: response }))
            }
        }
    }

这同样适用于除 iOS 设备之外的所有平台。对于iPhone 6,它重定向回 Microsoft 登录屏幕,在调试时我知道响应 url 中没有数据,而应该有access_tokenid_token等。当我点击使用 Outlook 登录时,还有一件事是iPhone X按钮,dialogApi 会打开一个窗口并一直闪烁,甚至不显示 Microsoft 登录屏幕。

这是dialogApi代码

const dialogLoginUrl = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/auth/outlook/login.html'
Office.context.ui.displayDialogAsync(
            dialogLoginUrl,
            { height: 70, width: 40 },
            (result) => {
                console.log("dsad5asd65asd65as6d5", { result })
                    loginDialog = result.value
                    loginDialog.addEventHandler(Office.EventType.DialogMessageReceived, processLoginMessage)
                    loginDialog.addEventHandler(Office.EventType.DialogEventReceived, processLoginDialogEvent)
                }
        )

标签: iosoutlookoauth-2.0azure-active-directoryoutlook-web-addins

解决方案


推荐阅读