在Firebase Microsoft帐户身份验证中从用户处获取电子邮件

问题描述 投票:0回答:1

我使用以下代码验证使用Microsoft帐户登录的Firebase用户:

  signInMicrosoft = () => {
    var provider = new firebase.auth.OAuthProvider('microsoft.com');
    provider.addScope('openid');
    provider.addScope('email');
    firebase
      .auth()
      .signInWithPopup(provider)
      .then(
        function(result) {
          console.log('result', result)
          var token = result.credential.accessToken;
          var user = result.user;
          var isNewUser = result.additionalUserInfo.isNewUser;
          //window.location.href = "/";
        }.bind(this)
      )
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        this.setState({
          loginError: true
        });
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    });
  }

但是,当我尝试使用以下代码访问用户的电子邮件时:

firebase.auth().onAuthStateChanged(user => {
  console.log('user', user) 
});

'user.email'条目设置为null。如何让Firebase以这种方式访问​​Microsoft帐户的电子邮件,以便将'user.email'设置为用户的电子邮件?

javascript firebase firebase-authentication microsoft-graph
1个回答
1
投票

我试过了。

我可以收到电子邮件地址。

看到:

此时,offline_access(“维护对您已授予其访问权限的数据的访问权限”)和user.read(“登录并阅读您的个人资料”)权限将自动包含在对应用程序的初始同意中。这些权限通常是正确的应用程序功能所必需的 - offline_access使应用程序可以访问刷新令牌,对本机和Web应用程序至关重要,而user.read可以访问子声明,允许客户端或应用程序随时间正确识别用户和访问基本用户信息

重要Microsoft身份验证库(MSAL)当前在授权和令牌请求中默认指定offline_access,openid,配置文件和电子邮件。这意味着,对于默认情况,如果您明确指定这些权限,Azure AD可能会返回错误。

provider.addScope();没有必要。

请尝试注释或删除provider.addScope();

    // provider.addScope('openid');
    // provider.addScope('email');

您应该检查您的Microsoft应用程序设置。

Microsoft Graph Permissions> Delegated Permissions设置User.Read。?

备注:

以下文档中的provider.addScope('mail.read');允许应用程序读取用户邮箱中的电子邮件。不允许该应用读取用户电子邮件地址。

© www.soinside.com 2019 - 2024. All rights reserved.