我能够获取用户的accessToken,并且正在使用带有 Authorization: Bearer <token>
头的调用 GET https://graph.microsoft.com/v1.0/me
。
但是,我在响应主体中得到的结果类似于:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
"value": [
{
"givenName": "Foo",
"surname": "Bar",
"displayName": "Foo Bar",
"id": "b41efha115adcca29",
"userPrincipalName": "email.address@outlook.com",
"businessPhones": [],
"jobTitle": null,
"mail": null,
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null
}
]
}
mail
属性值为null
,而此响应体中的userPrincipalName
恰好是用户的电子邮件地址。但是,根据Microsoft的文档:
尽管UPN和电子邮件具有相同的格式,但用户的UPN值可能与用户的电子邮件地址相同也可能不同。
在启动用户登录请求时,我们请求"user.read"
和"email"
范围。我们使用MSAL.js库获取访问令牌,我们的代码类似于:
login (): ng.IPromise<IMicrosoftOAuthResponse> {
const prom = this.userAgentApplication.loginPopup(["user.read", "email"])
.then((idToken) => {
return this.userAgentApplication.acquireTokenSilent(["user.read", "email"]);
})
.then((accessToken) => {
// at this point, we have the accessToken and make a call to the graph api
});
return this.$q.when(prom);
}
我该如何获取此处用户的实际电子邮件地址?
userPrincipalName
始终是用户的电子邮件。其主要含义是:UPN是基于互联网标准RFC 822的用户互联网样式登录名。根据惯例,这应映射到用户的电子邮件名称。一般格式为“alias@domain”。对于工作或学校帐户,域必须存在于租户的已验证域集合中。 - vishwarajanandmail
属性应该是用户的实际电子邮件地址,但它返回了null
。 - bioball