You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Office Add-In问题:Office.auth.getAccessToken在移动浏览器失效

Office Excel Web Addin iPadOS SSO认证问题排查与解决

问题背景

基于Visual Studio 2022开发的Office Excel Web Addin,实现了Office SSO+服务器端代表流的认证逻辑,核心通过Office.auth.getAccessToken从Office Context获取令牌。该逻辑在Windows、MacOS桌面版Excel及桌面浏览器中运行正常,但在iPadOS的Edge、Safari等移动浏览器中完全失效,多款Chromium内核浏览器测试后问题仍存在。

核心认证代码

async function GetAccessToken() {
    try {
        console.log("Start to fetch behalf token");
        var behalfToken = "";
        var accessToken = "";
        await Office.auth.getAccessToken({
            allowSignInPrompt: true,
            allowConsentPrompt: true,
            forMSGraphAccess: true
        }).then(officeResponse => {
            if (officeResponse.error) {
                console.log(response);
            } else {
                behalfToken = officeResponse
            }
        });
        console.log("Got behalf token");

        console.log("Fetch access token")
        const url = "https://myazurewebiste-site1.atempurl.com/onbehalf"
        const headers = { 'Authorization': `${behalfToken}` };

        accessToken = await Http("GET", url, headers)

        console.log("We got accessToken")

        return accessToken.access_token;

    } catch (error) {
        console.error("发生错误:", error.message);
    }
}

iPadOS Edge调试错误日志(翻译后)

错误信息:Office.auth.getAccessToken调用失败,错误码:13003,错误描述:无法完成认证操作,当前环境不支持所需的认证流程。

针对性解决方法

  • 处理移动端弹窗拦截:iPadOS的Safari和Edge对第三方弹窗有严格限制,需将认证流程绑定到用户主动触发的交互(如按钮点击),避免页面加载自动执行导致弹窗被拦截。
  • 调整SSO参数配置:移除forMSGraphAccess: true,改为手动指定Graph资源ID,避免移动端环境下的权限请求限制:
    await Office.auth.getAccessToken({
        allowSignInPrompt: true,
        allowConsentPrompt: true,
        resource: "https://graph.microsoft.com"
    })
    
  • 校验跨域配置:确保Azure AD应用的重定向URI已包含移动端浏览器的允许地址,同时在Addin的manifest.xml中配置正确的AppDomains,避免跨域请求被拦截。
  • 适配Cookie策略:在后端响应中添加SameSite=None; Secure的Cookie属性,适配iPadOS严格的跟踪保护策略,维持认证会话。
  • 添加移动端错误降级:针对错误码13003补充降级逻辑,引导用户切换环境或使用手动登录:
    catch (error) {
        if (error.code === 13003) {
            console.error("移动端环境不支持SSO,请使用桌面版Excel或手动登录");
            // 此处添加手动登录逻辑
        } else {
            console.error("发生错误:", error.message);
        }
    }
    

内容的提问来源于stack exchange,提问作者Kelaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:54:51