I am able to get access to a user's accessToken, and am making a call to GET https://graph.microsoft.com/v1.0/me
with an Authorization: Bearer <token>
header.
However, in the response body I'm getting something like this:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
"value": [
{
"givenName": "Foo",
"surname": "Bar",
"displayName": "Foo Bar",
"id": "b41efha115adcca29",
"userPrincipalName": "[email protected]",
"businessPhones": [],
"jobTitle": null,
"mail": null,
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null
}
]
}
The mail
property is null
, and the userPrincipalName
in this response body happens to the be the user's email address. However, there's this from Microsoft's docs:
Although the UPN and email share the same format, the value of the UPN for a user might or might not be the same as the email address of the user.
When initiating the login request of the user, we're requesting for the "user.read"
and "email"
scopes. We're using the MSAL.js library to obtain the access token, and our code reads something like this:
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);
}
How do I get the actual email address of the user here?
userEntity
,userPrincipalName
seems to always be the email of user. Main gist of it: 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". For work or school accounts, the domain must be present in the tenant's collection of verified domains. – MaterfamiliasuserPrincipalName
which does not have email instead? – Materfamiliasrecommended
to keep the UPN same as email. But it seems more like a general practice instead. This blog also suggests that many applications are actually using the UPN as email. – Materfamiliasmail
property should be the user's actual email address, but it's returningnull
. – Crissman<username>_live.com#EXT#@<username>live.onmicrosoft.com
. Using the same query (same headers etc.) just with a Bearer token from Graph Explorer I got just the email (<username>@live.com
) as UPN. I tried playing with different permissions, but this did not change the returned value. – Jackfish