即使offline_access可用,msal.js也无法获取refresh_token,我可以在返回调用中看到它

问题描述

我使用msal.js来访问Microsoft Graph Api,并且已将其用于帖子部分。

正如您在图像上看到的那样,当我进行控制台日志记录时,我在响应有效负载中得到了refresh_token,但在实际输出中却没有得到刷新,我该怎么做?

enter image description here

enter image description here

然后,我需要这个refresh_token,因为他们对其Microsoft帐户进行身份验证的位置与实际显示数据的位置不同。 让我解释一下

我们是一家信息屏幕公司,我们需要它来显示将日历插入演示文稿中时进行身份验证的人员的日历。 因此流程如下:

  1. 他们安装应用程序并使用其Microsoft 365帐户登录,以使我们能够访问此数据。 (这是返回刷新令牌和访问令牌的部分。)
  2. 他们转到演示文稿,然后将应用程序插入要显示的区域。
  3. 现在可以在世界任何地方摆放的实际显示器上显示日历。

但是1小时后,会话将终止,因此我们需要生成一个新的access_token,为此,我们需要refresh_token。

在loginPopup的步骤中,我可以看到有一个refreshToken 但是,当我使用数据时,它也消失了,我也尝试静默地请求令牌 我还更新了应支持它的最新版本的msal-browser.min.js 2.1版。

async function signInWithMicrosoft(){
    $(".notification_box",document).hide();
    $("#table_main",document).show();
    const msalConfig = {
        auth: {
            clientId: '{CLIENTID}',redirectUri: '{REDIRECTURI}',validateAuthority: false
        },cache: {
            cacheLocation: "sessionStorage",storeAuthStateInCookie: false,forceRefresh: false
        },};

    const loginRequest = {
      scopes: [
            "offline_access","User.Read","Calendars.Read","Calendars.Read.shared"
      ],prompt: 'select_account'
    }

    try {
        const msalClient        = new msal.PublicClientApplication(msalConfig);
        const msalClientLoggedIn= await msalClient.loginPopup(loginRequest).then((tokenResponse) => { console.log(tokenResponse); });
        msalClientAccounts      = msalClient.getAllAccounts();
        var msalInsertAccount   = true;
        var tableMainAsText     = $("#table_main",document).text();
        if(typeof msalClientLoggedIn.idTokenClaims !== 'undefined'){
            if(tableMainAsText.indexOf(msalClientLoggedIn.idTokenClaims.preferred_username)>-1){
                msalInsertAccount = false;
            }

            if(msalInsertAccount){
                var tableRow = "<tr>"+
                                    "<td>"+msalClientLoggedIn.idTokenClaims.name+" ("+msalClientLoggedIn.idTokenClaims.preferred_username+") <span style='display: none;'>"+msalClientAccounts[0].username+"</span><input type=\"hidden\" name=\"app_config[exchange_online][]\" class=\"exchange_online_authed_account\" value=\""+msalClientLoggedIn.idTokenClaims.preferred_username+","+msalClientLoggedIn.idTokenClaims.name+"\" /></td>"+
                                    "<td class=\"last\"><a href=\"#\" onclick=\"removeMicrosoftAccount(this);\">Fjern adgang</a></td>"+
                                "</tr>";
                $("#table_body",document).append(tableRow);
                $("#table_foot",document).hide();
            }
        }
    }catch(error){
        $(".notification_box",document).show();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)