Microsoft graph api 通过msal.js从经典JavaScript调用Graphi API

Microsoft graph api 通过msal.js从经典JavaScript调用Graphi API,microsoft-graph-api,sharepoint-online,azure-ad-graph-api,msal,msal.js,Microsoft Graph Api,Sharepoint Online,Azure Ad Graph Api,Msal,Msal.js,我尝试通过msal.js进行静默登录,然后尝试调用graph api,但总是出现403错误。当我通过jwt.ms解密我的访问令牌时,我可以看到访问者是正确的,但作用域显示错误。希望有人能帮助我 我的代码 let config = { auth: { clientId: _spPageContextInfo.spfx3rdPartyServicePrincipalId, authority: `https://login.microsoftonline.com/${

我尝试通过msal.js进行静默登录,然后尝试调用graph api,但总是出现403错误。当我通过jwt.ms解密我的访问令牌时,我可以看到访问者是正确的,但作用域显示错误。希望有人能帮助我

我的代码

let config = {
    auth: {
      clientId: _spPageContextInfo.spfx3rdPartyServicePrincipalId,
      authority: `https://login.microsoftonline.com/${_spPageContextInfo.aadTenantId}`,
      redirectUri: 'https://xxx.sharepoint.com/sites/xxx-Dev/Pages/myportal.aspx',
      validateAuthority: false,
      postLogoutRedirectUri: window.origin,
    },
    cache: {
      cacheLocation: 'localStorage',
      storeAuthStateInCookie: true
    }
  }

  let myMSALObj = new Msal.UserAgentApplication(config)
  let graphConfig = {
    graphGroupEndpoint: "https://graph.microsoft.com/v1.0/groups"
  }

  let request = {
    scopes: ["https://graph.microsoft.com/.default"]
  }


  myMSALObj.handleRedirectCallback(response => { console.log(response) });
  //const idTokenScope = { scopes: [_spPageContextInfo.spfx3rdPartyServicePrincipalId] }

  const handleError = (error) => {
    if (error.errorCode === 'consent_required'
      || error.errorCode === 'interaction_required'
      || error.errorCode === 'login_required') {
      //myMSALObj.loginRedirect(idTokenScope);
      myMSALObj.loginRedirect(request);
      return;
    }
    throw error;
  };

  const getToken = () => {
    const date = new Date();
    const user = myMSALObj.getAccount();
    if (!user) {
      //myMSALObj.loginRedirect(idTokenScope);
      myMSALObj.loginRedirect(request);
      return;
    }
    //myMSALObj.acquireTokenSilent(idTokenScope).then(response => {
    myMSALObj.acquireTokenSilent(request).then(response => {
      console.log(`${date.toLocaleTimeString()}`, response.accessToken);
      callMSGraph(graphConfig.graphGroupEndpoint, response.accessToken, graphAPICallback)
    }).catch(handleError);
  }
  getToken()

  function callMSGraph(theUrl, accessToken, callback) {
    var xmlHttp = new XMLHttpRequest()
    xmlHttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200)
        callback(JSON.parse(this.responseText))
    }
    xmlHttp.open("GET", theUrl, true)
    xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken)
    xmlHttp.send()
  }
  function graphAPICallback(data) {
    document.write(JSON.stringify(data, null, 2))
  }
我的解码令牌

我的应用程序许可
有两种权限:一种是应用程序权限,另一种是委托权限。和“
https://graph.microsoft.com/.default
“用于申请许可证

通过从浏览器进行交互式登录,您将被要求提供您的凭据。这样,您将为自己获得一个访问令牌,该令牌具有委托权限。我们称之为

但是,由于您没有在请求范围中设置任何必需的委托权限,Azure AD只会返回一个具有基本委托权限的访问令牌(openid、通过电子邮件发送配置文件)


顺便说一下,如果您只想获得具有应用程序权限的访问令牌。您只需要使用来获取令牌