获取 Microsoft 用户的电子邮件地址

Get the email address of a Microsoft user

我想在我的 mean-stack 应用程序中添加一个 login with Microsoft 按钮。所以我正在使用 passport-microsoft.

通过使用passport.authenticate('microsoft', { scope: ['User.Read'] }),我可以得到这些信息:@odata.contextgivenNamesurnamedisplayNameiduserPrincipalNamebusinessPhonesjobTitlemailmobilePhoneofficeLocationpreferredLanguage。对我来说,我的 userPrincipalName 是一个电子邮件地址,而我的 mailnull.

我已按如下方式设置应用程序,但如果我写 passport.authenticate('microsoft', { scope: ['User.Read', 'email'] }),身份验证会给我一个 "invalid scope" 错误。

有人知道如何设置我的应用程序并请求获取用户的电子邮件地址吗?

如果您查看 the documentation for the user object,您会发现以下内容:

mail

String

The SMTP address for the user, for example, "jeff@contoso.onmicrosoft.com". Read-Only. Supports $filter.

userPrincipalName

String

The user principal name (UPN) of the user. The UPN is an Internet-style login name for the user based on the Internet standard RFC 822. By convention, this should map to the user's email name. The general format is alias@domain, where domain must be present in the tenant’s collection of verified domains. This property is required when a user is created. The verified domains for the tenant can be accessed from the verifiedDomains property of organization. Supports $filter and $orderby.

所以 mail 属性 似乎不一定代表用户的电子邮件地址,而是专门用于 Outlook 和 Office 365。它也不是必需的 属性 创建用户时。相反,您应该依赖 userPrincipalName 作为用户的电子邮件地址。

至于你的示波器错误,你应该分享完整的错误信息。